String: a common data type in programming. It is not a simple basic data type, but a composite data type.
Its biggest feature is that it cannot be modified or changed after creation.
It can be used without import, and this class does not need to be extended. It is defined as a final class (final class) and cannot be inherited by other classes.
String s1 = String("java01" String s2 = "java01";
The above two methods are used to create a String. The results are the same, but the second method is relatively simple.
The difference between the two creation methods:
S1 has two objects in memory (one is new and the other is string objects in heap memory)
S2 has only one object in the memory (this character object exists in the String constant pool)
S2 is a class type variable pointing to an object, and "java01" is an object. A string is a special object and a constant. It cannot be changed after initialization. All objects in double quotation marks are objects (exist in the constant pool)
String s1 = "java01" s1 = "java02" System.out.println(s2);
The final result is java02, but not the java01 object changed, but the s1 point is changed to the java02 object. The java01 object still exists in the memory.
String s1 = String("java01" String s2 = String("java01" System.out.println(s1==s2); System.out.println(s1.equals(s2));
= And equals are both compared to the memory address value of the object, but the equals method is rewritten by the String class and defines its own comparison method.
String s1 = "java01"= String("java01"= "java01"==s2);System.out.println(s1==s3);