Java basic knowledge summary

Time:2020-3-25

1. Variable type
Basic type: the variable of basic type “holds” a value; reference type: the variable of reference type “points” to an object.

2. Basic data type
The basic data type is the type that the CPU can perform operations directly. The basic data types in Java are:

  • Integer type: byte short int long
  • Float type: float double
  • Character type: char
  • Boolean type: Boolean

3. = = and equals

  • ==Judge whether two variables or instances point to the same memory space, and equals judge whether two variables or instances point to the same memory space
  • ==Compare memory address and equals content

4. Break and continue

Break out of the current loop, continue to end this loop in advance, and continue to execute the next loop.

Object-oriented

Object oriented programming is a kind of programming method that maps the real world to the computer model through the way of object.

5. Classes and instances

  • Class is an object template, which defines how to create an instance. Instance is an object instance, which is created according to class.

6. Parameter transfer

  • Parameter transfer of basic type refers to the copy of caller’s value, and subsequent modification of both sides does not affect each other; parameter transfer of reference type refers to the same object, and modification of this object by either side will affect the other.

7. Construction method

  • The constructor is used to initialize instances. If a class does not have a constructor defined, the compiler will automatically generate a default constructor for us. If a constructor is defined, the compiler will no longer automatically create a default constructor. Note: in Java, for any class constructor, the first line of statement must be the constructor calling the parent class. If the constructor of the parent class is not explicitly called, the compiler will automatically add a super () for us.

8. Method overload method rewrite

  • Method names are the same, but their parameters are different, which is called method overload. The purpose of method overloading is that methods with similar functions use the same name, which is easier to remember and easier to call.
  • In the inheritance relationship, if a subclass defines a method with the same signature as the parent method, it is called override.

9. inheritance

  • Inheritance is a very powerful mechanism in object-oriented programming, which can reuse code. Java allows only one class to inherit from one class, so a class has only one parent class. Only object is special. It has no parent class. The child class cannot access the private field or private method of the parent class. The subclass will not inherit any construction methods of the parent class. The default construction methods of the subclass are generated automatically by the compiler, not inherited.

10. polymorphism

  • Polymorphism means that for a type of method call, the real method it executes depends on the actual type of method at runtime. Polymorphism has a very powerful function, which is to allow more types of subclasses to be added to realize function extension without modifying the code based on the parent class.

11. abstract class

  • A class decorated with abstract is an abstract class. Cannot instantiate an abstract class. Declaring a method as abstract means that it is an abstract method and does not implement any method statements. Abstract class itself is designed to be used only for inheritance. Abstract class can force subclass to implement its defined abstract method, otherwise compilation will report an error. Therefore, an abstract method is actually equivalent to defining a “specification”.
  • Non abstract methods can be defined in abstract classes.

12. interface

  • If an abstract class has no fields and all methods are abstract methods, it can be rewritten as interface. The so-called interface is a pure abstract interface more abstract than an abstract class, because it cannot even have fields. Because all methods defined by the interface are public abstract by default, these two modifiers do not need to be written out.
  • A class can implement multiple interfaces.
  • In the interface, the default method can be defined, and the implementation class can not override the default method. The purpose of the default method is that when you need to add a method to an interface, it involves modifying all subclasses. If the default method is added, then the subclass does not need to be modified completely, only the new method needs to be rewritten where it needs to be rewritten.

13. Final keyword

  • The final decorated class can prevent inheritance;
  • The final decorated method can prevent being rewritten;
  • The final decorated property must be initialized when the object is created and cannot be modified later.

14. Static keyword

  • Static decorated member variables and methods, subordinate to the class
  • Common variables and methods are subordinate to objects
  • Static methods cannot call non static members, compilation will report an error
  • Instance objects can access static fields only because the compiler can automatically convert them to class names and static fields according to instance types. It is recommended to use the class name to access static fields.

15. Local variables

  • The variables defined in the method are called local variables, and the scope of local variables starts from the variable declaration and ends at the corresponding block. Method parameters are also local variables.

16.String

  • The string is represented by a char [] array inside the string,

An important feature of Java strings is that they are immutable.

public class Main {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }
}

From the comparison results, both are true. This is because the java compiler will automatically put all the same strings into the constant pool as an object during compilation. Naturally, the references of S1 and S2 are the same.
So it’s a coincidence that this = = comparison returns true. In another way, = = comparison will fail:

public class Main {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "HELLO".toLowerCase();
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }
}

Why is a true and a false? The reason is that to lowercase(), which points S2 to a string that becomes lowercase.

17. StringBuffer and StringBuilder

  • StringBuilder, a variable object, can preallocate buffer, so that when adding characters to StringBuilder, new temporary objects will not be created.
  • StringBuffer is a thread safe version of StringBuilder in the early Java. It ensures that multiple thread operations StringBuffer is safe through synchronization, but synchronization will lead to a decrease in execution speed.

18. Automatic packing and unpacking

  • Automatic boxing and unboxing only happen in the compile phase, in order to write less code;
  • Boxing and unboxing will affect the execution efficiency of the code, because the compiled class code strictly distinguishes between the basic type and the reference type. In addition, NullPointerException may be reported during autounboxing.

19. enumeration

  • Java uses enum to define enumeration type, which is compiled by compiler as final class XXX extends enum { }
  • Enumeration class is a reference type;
  • Each constant of enumeration type has only one unique instance in the JVM, so you can directly use = = to compare.
  • The defined enum type always inherits from java.lang.enum and cannot be inherited;
  • Only the instance of enum can be defined, but the instance of enum cannot be created through the new operator;

20. exceptions
Throwable is the root of the exception system, which inherits from object. Throwable has two systems: error and exception. Error indicates a serious error, for which the program is generally powerless. Exception is a runtime error, which can be caught and processed.
21. reflection

  • Java’s reflection means that the program can get all the information of an object at runtime. Because the JVM creates a corresponding class instance for each loaded class, and saves all the information of the class in the instance, including class name, package name, parent class, implemented interface, all methods, fields, etc., if we get a class instance, we can get all the information of the class corresponding to the instance through this class instance. This method of obtaining class information through a class instance is called reflection.

22. notes

  • Annotation is a kind of “metadata” used as annotation. The essence of annotation is an interface that inherits annotation interface.

23. generic

  • The essence of generics is to parameterize the type (without creating a new type, control the type of parameter specific restriction through different types specified by generics). That is to say, in the process of using generics, the data type of operation is specified as a parameter. This parameter type can be used in classes, interfaces and methods, which are called generic classes, generic interfaces and generic methods respectively.