The object of the string class is a string constant, and everything seems to change the operation of a string object, but it only changes the string referenced by the string reference variable.
The strings in Java are stored in a common storage pool where references point to the corresponding location in the storage pool, where the compiler can let the string be shared, that is, two different references that can point to the same string constant, but the physical storage of the string is actually only one copy.
To determine whether two strings are equal, be sure to use the Equals method. "= =" can only determine whether two references point to the same string, and the same string may be stored in different physical storage locations.
In fact, only string constants are shared, and the results of operations such as + or substring are not shared. (Not absolute)
1 String s= "People"; 2 String t= "People"; 3 char c[]={' P ', ' e ', ' o ', ' P ', ' l ', ' E '}; 4 System.out.println (s== "PE" + "O" + "ple"); True5System.out.println (T.equals (c)); False6 System.out.println (t.equals (new String ("people")); True
In summary, do not use "= =" to determine the equality of strings in order to avoid errors.
The Equals method of string and = =