Java basics summary

Posted by balistic on Thu, 03 Mar 2022 23:44:56 +0100

Characteristics of class inheritance - construction method

  • The name of the constructor is consistent with the class name. Therefore, a subclass cannot inherit the constructor of its parent class.
  • Constructor is used to initialize member variables. Therefore, in the initialization process of a subclass, the initialization action of the parent class must be performed first. There is a super() in the construction method of the subclass by default, which means that the construction method of the parent class is called. It can be used by the subclass only after the parent class member variable is initialized.
class Fu {
  private int n;
  Fu(){
    System.out.println("Fu()");
  }
}

class Zi extends Fu {
  Zi(){
    // super(), call the parent class constructor
    super();
    System.out.println("Zi()");
  }  
}
public class ExtendsDemo07{
  public static void main (String args[]){
    Zi zi = new Zi();
  }
}
Output result:
Fu()
Zi()

Meaning of super and this

super: represents the storage space identification of the parent class (which can be understood as the reference of the parent class).
this: represents the reference of the current object (whoever invokes it represents who).

abstract class

Origin: the method in the parent class is rewritten by its subclasses, and their implementations are different. Then the method declaration and method body of the parent class, only the declaration has meaning, while the method body has no meaning. We call methods without method bodies abstract methods. Java syntax stipulates that a class containing abstract methods is an abstract class.
Abstract method: use the abstract keyword to modify the method, which becomes an abstract method. The abstract method contains only one method name, but no method body.

Initialization sequence

Static attribute initialization – static method block initialization – common attribute initialization – common method block initialization – constructor initialization.

public class LifeCycle {
	// Static properties
	private static String staticField = getStaticField();
	// Static method block
	static {
		System.out.println(staticField);
		System.out.println("Static method fast initialization");
	}
	// General properties
	private String field = getField();
	// Common method block
	{
		System.out.println(field);
	}
		// Constructor
		public LifeCycle() {
		System.out.println("Constructor Initializers ");
	}
	public static String getStaticField() {
		String statiFiled = "Static Field Initial";
		return statiFiled;
	}
	public static String getField() {
		String filed = "Field Initial";
		return filed;
	}
	// Main function
	public static void main(String[] argc) {
		new LifeCycle();
	}
}

final

Exception:


Classify and analyze Throwable and its subclasses:

The difference between ArrayList and Vector

  1. ArrayList non thread safe, Vector thread safe
  2. After the capacity of ArrayList is expanded, the array length is increased by 50% and the Vector is doubled.

String uses the difference between equals and = =

  1. If the object is different, return "true =" equals = "and return the same content
String s1 = new String("java");
String s2 = new String("java");
 
System.out.println(s1==s2);            //false
System.out.println(s1.equals(s2));    //true
  1. For the same object, "= =" and equals have the same result
String s1 = new String("java");
String s2 = s1;
 
System.out.println(s1==s2);            //true
System.out.println(s1.equals(s2));    //true

The equals method in the String class can not only use = = to judge whether the memory addresses of objects are equal, but also return true if they are equal. If the previous judgment is not true, then judge whether the object in parentheses is of String type, then judge whether the lengths of two String objects are equal, and finally judge whether the contents are equal. If they are equal, return true.

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

The object '' is null

  1. String s=null;//null is the unallocated heap memory space
  2. String a;// A memory space was allocated and no objects were stored
  3. String a="";// A memory space is allocated and a string object is stored
    The first exception will appear, and the second will output null The third will output a

Topics: Java