Learn JAVA from scratch (basic introduction)

Keywords: Java Eclipse

1. Build JAVA development environment

Uninstall JDK (please ignore those not installed)

  • Delete the JDK installation directory

  • Delete JAVA_HOME and CLASS_PATH environment variable

  • Delete PATH and JAVA_HOME and CLASS_PATH related environment variables

Install JDK

  • Go to Oracle's official website to download JDK or go to some domestic image websites to download JDK installation files

  • Install the JDK into the machine

  • New environment variable JAVA_HOME, specify the installation directory whose path is JDK

  • Update environment variable PATH and add% JAVA_HOME\bin%, and% JAVA_HOME\jre\bin%

  • Open the DOS command window of the computer, enter the java -version and javac commands to judge whether the java environment has been installed

2. What are compiled and interpreted programming languages

Compiled type

Use a special compiler to compile the high-level language source code into machine code that can be executed by the hardware of the platform at one time, and package it into the format of executable program recognized by the platform. It can be understood that there is an interpreter that compiles all the source code into mechanical code that can be executed by hardware and executes it directly. There is no need to compile again during operation (fast)

Interpretive type

Use a special interpreter to compile the source code line by line into specific hardware executable mechanical code and execute it directly

3. First JAVA application

// Execute javac HelloJava.java on the dos command line
// Execute java HelloJava
// Output result: Hello JAVA
public class HelloJava{
public static void main(String[] args){
     System.out.println("Hello JAVA");
  }
}
// To learn more about additive bacteria: click the link to join [Java technology exchange - 12 bacteria]: https://jq.qq.com/?_ wv=1027&k=c6vwargj

4. What are the keywords in JAVA

abstract   assert   boolean   break   byte   case
catch   char   class   const   continue   default
do   double  else   enum   extends   final   finally
float   for   goto   if   implements   import   instanceof
int   interface   long   native   new   package   private
protected   public   return   short   static   strictfp
super   switch   synchronized   this   throw   throws
transient   try   void   volatile   while

5. Use of notes

// This is a single line comment
/*
  This is a multi line comment. I'm the first line
  This is a multi line comment. I'm the second line
*/

/**
  This is a DOC document comment
*/

6. Rules for defining identifiers

 

//Identifiers defined in JAVA can only be letters [a-z][A-Z], underscores The dollar starts with the sign $
  // Correct instance
  int name;
  int Name;
  String _str;
  String $str;

  // Error example
  int *1;
  int #1;

7. Basic data type rules

8 basic data types of JAVA

 

/**
    bit is the smallest storage unit of a computer
    1 Bytes occupy 8 bits, that is, 1byte = 8bit
  */
  // Integer type
  byte Occupy 1 byte, range 2^7 reach (2^7)-1
  short Occupy 2 bytes
  int Occupy 4 bytes
  long Occupy 8 bytes
  // Floating point type
  float Occupy 4 bytes
  double Occupy 8 bytes
  // Character type

  char Occupy 2 bytes
  // Boolean type, with only true or false values
  boolean Occupy 1 place

Data type interview questions

  • What data type does the bank amount need to use
    BigDecimal class to store the amount

  • Why can't floating point numbers be used to calculate amounts
    Because it is not accurate enough, accuracy will be lost

Basic data type conversion rules

  • Automatic conversion rules
    Upward transformation means that those with low precision will be transformed into those with high precision. After long and double are calculated, the result is double.

  • Cast rule

// Put parentheses (data type) [variable name] before the variable to be cast
 // Forced conversion is only required for downward transformation (that is, forced conversion is required for conversion from high precision to low precision)
 // Attention should be paid to the loss of precision when casting
 long long1 = 10;
 int int1 = (int)a1;

Variable, constant, scope

 

// The scope of local variables is limited to the current method
  // Member variables can only be used when an instance of this class is created
  // Class variables can be called by class and act on the current class

  // A variable that uses the final modifier becomes a constant
  // Variable, as the name suggests, a value that can be re assigned multiple times

Arithmetic operator

Self increasing and self decreasing operator

 

int a = 10;
 int b = a++; // First assign a value and then add 1. The value is 10. After the end of the line, a is 11
 int c = ++a; // Add 1 before copying, and the value is 12

And or not operator

 

// And & &, both are true, and & & are open and. As long as the left is false, the right will not be judged
  // Or 𞓜, it is false only if both are false, otherwise it is true
  // No, Invert false to true and true to false

Bitwise operators & |^ ~ < > > > >

& | ^ ~
  /*
    The bottom layer of the computer will convert it into complement form during operation
    1,The complement of a positive number is equal to its original code
    2,The complement of a negative number is equal to its original code (inverse + 1), and when the complement of a negative number is converted to the original code, it needs (- 1 inverse), that is, inverse operation
    3,During the conversion between complement and original code, the highest symbol bit does not need to be reversed
    4,When using ~ to negate, the symbol should also be negated
    5,Remember some details when moving left or right, 5 > > 3 = 5 / (2 ^ 3), 5 < < 3 = 5 * (2 ^ 3)
  */
  // Application of bit operation 1: judge odd and even numbers, a number & 1. If the result is 1, it is odd, otherwise it is even
  int num = 10;
  String result = (num & 1)==0 ? "even numbers" : "Odd number";
  /*
    Application of bit operation 2: use XOR ^ to complete the value exchange between two numbers
    Principle: if a number is exclusive or itself, and then exclusive or another number, it will become that other number. For example, 5 ^ 5 ^ 8 = 8. Because the result after 5 ^ 5 is 0, the result when it is ^ 8 is 8
    Two numbers a and b are exchanged, a = a^a^b, b = b^b^a; You can see a rule in which there is an a ^ b on the right of both equations

  */
  int a = 10;
  int b = 20;
  // Because there is a ^ b on the right side of the equation
  a = a ^ b;
  // When a equals a^b, b only needs to use its own ^ a once to get the value of A
  b = b ^ a;
  // At this time, a only needs to ^ his original value again to get the value of b. his original value has been assigned to b, so ^ b again
  a = a ^ b;
  System.out.println("At this time a The value of is equal to:" + a + " b The value of is equal to:" + b);

javadoc generates java documents

 

@author Author name
  @version Version number
  @since  Indicates the earliest used jdk edition
  @parram Parameter name
  @return Return value
  @throws Exception thrown

  generate javadoc Commands to use when: javadoc -encoding utf-8 -charset utf-8

Process control

switch

 

// Only byte, short, int and char can be written in the switch statement. String can be used in jdk1.7

recursive thinking

 

Recursion means that the method calls itself, but the space complexity of recursive call is high, which is prone to stack overflow
  For example, use recursion to write a program that can calculate factorials

  public int factorial(int baseNum) {
    if (baseNum == 1){
      return 1;
    }else {
      return baseNum * factorial(baseNum - 1);
    }
  }

java memory stack, heap, method area, constant pool

Stack

  • Reference (address) of storage object

  • Store variables of basic types (including values)

heap

  • Objects and arrays stored by new

Method area

  • Store all class files

  • Method for storing static modification and its variables

Three initializations and basic characteristics of arrays

initiate static

int[] arr1 = {1,2,3,4};
String[] arr2 = {new String("s1"),new String("s2")};

Dynamic initialization (including default initialization and default values of corresponding data types)

int[] arr2 = new int[10];

Basic characteristics of array

  • Once the array length is determined, it cannot be changed

  • Arrays can only be fixed types, not mixed types (each element in the array must be of the same type)

  • The elements in the array can be basic types or reference types

  • The objects created by the array are stored in the heap and the references are stored in the stack

  • And the array itself is an object

Understanding of two-dimensional array

It can be understood that an array contains multiple one-dimensional arrays

Selective sorting and bubble sorting of arrays

 

// Bubble sorting
  public static void bubbleSort(int[] arr) {
        // Determine the number of cycles
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[i] < arr[j]){
                    // exchange
                    arr[i] = arr[j] ^ arr[i];
                    arr[j] = arr[j] ^ arr[i];
                    arr[i] = arr[j] ^ arr[i];
                }
            }
        }
    }

  // Select sort
  public static void selectSort(int[] arr) {
        // Determine the number of cycles
        for (int i = 0; i < arr.length - 1; i++) {
            // Each cycle needs to get a maximum or minimum value
            for (int j = i + 1; j < arr.length; j++) {
                if(arr[i] < arr[j]){
                    // exchange
                    arr[i] = arr[j] ^ arr[i];
                    arr[j] = arr[j] ^ arr[i];
                    arr[i] = arr[j] ^ arr[i];
                }
            }
        }
    }

Permission modifier

 

Permission modifier       This category       Current package      Subclasses under different packages      Different packages
private           √
default(default)     √         √
protected         √         √            √
public            √         √            √                  √

In polymorphism, the compilation of methods and methods is on the left, the operation is on the right, and the compilation and operation of variables are on the left

Remember that static modified methods cannot be rewritten, so when calling static modified methods, the compilation run will look at the left, while variables cannot be rewritten, so the compilation run will also look at the left

public class A {
    public static void main(String[] args) {
        Father father = new Son();
        System.out.println(father.a);
        System.out.println(father.b);
        father.show();
        father.staticShow2();
    }
}

class Father{
    int a = 10;
    static int b = 20;
    public void show(){
        System.out.println("Member method of parent class");
    }

    public static void staticShow2(){
        System.out.println("Static method of parent class");
    }
}
class Son extends Father{
    int a = 100;
    static int b = 200;
    @Override
    public void show(){
        System.out.println("Member methods of subclasses");
    }
    public static void staticShow2(){
        System.out.println("Static methods of subclasses");
    }
}

The difference between super keyword and this keyword

super

  • super calls the constructor of the parent class, which must be placed in the first line of the constructor

  • super must only appear in member methods or constructor methods of subclasses

  • super and this cannot call constructor at the same time! (because it will be initialized many times, the data is unsafe)

The difference between this and super

  • this is a reference to the current object

  • super is a reference to the parent class object

Posted by iamtheironman on Sat, 20 Nov 2021 01:49:42 -0800