String feature parsing and String feature Parsing
String
1. Immutable
String is the object of the encapsulated Char array.
Char [] a = {'h', 'E', 'l', 'l', 'O '};
String s = new String ();
String s = "hello ";
In memory, data stored in arrays is essentially data. The array length is immutable, and the created String object is immutable, but the variable is variable.
The value of s variable can be changed, and the string "hello" is immutable.
2. Comparison
String is a reference type. Use = to determine whether it is the same object.
All operations performed by using symbols are completed in the stack memory, such as "=" and "= ".
Therefore, compare whether the referenced address or memory address is equal
Because the strings are non-mutable, It is very memory-consuming to create strings repeatedly. Therefore, in the memory, a specific area is specified to store various constants, in the constant area, there is a special area to store string constants, called the String constant pool. It has a feature that strings are unique.
For example,
String s1 = "hello ";
String s2 = "hello ";
S1 = s2 true
When s1 is created, the string constant pool has one "hello". When s2 is created, the constant pool is no longer created because it is also "hello, s2 points directly to the created constant
So s1 = s2 true
String s3 = new String ("hello ");
String s4 = new String ("hello ");
As long as you see the object created by new, it must be in the heap memory. new indicates applying for a new space for storage.
S1 = s3 false
S3 = s4 false
S5 = "he" + "llo ";
S6 = "he ";
S6 + = "llo ";
S7 = s1;
S5 is obtained by adding two constants, constant + constant = constant. This process is completed directly during the compilation process and does not need to be executed. Therefore, when the program is running, s5 is equal to "hello"
So s1 = s5 true
The s6 value is a variable obtained from a variable + constant. For a program, the variable is known only when the program is running, and the value obtained at this time is definitely not in the constant pool. Therefore, the value produced by s6 is in the heap memory, while s1 is in the constant pool.
Therefore, s1 = s6 false
S7: Yes, the assignment directly assigns values to the reference address of s1.
So s1 = s7 true