1. Code and running results
in the In java, a string constant with the same content ("Hello") holds only one copy to conserve memory, so s0,s1,s2 actually refers to the same object.
When the compiler compiles a s2 sentence, It strips out the "+" number and connects the two strings directly to a string ("Hello"). This optimization work is done automatically by the Java Compiler.
When you create a string object directly using the new keyword, the value is the same (all "Hello"), but it is still two separate objects.
2. Code and running results
Assigning a value to a string variable means: two variables (s1,s2) now references the same string object "a"!
The contents of the string object are read-only, and the value of the S1 variable is modified with "+", in effect, a new string object is obtained with the content "ab", which is independent of the object "a" referenced by the original s1, so S1==s2 returns false;
The "ab" string in the code is a constant that refers to a string that is not related to the "ab" object referenced by s1.
The String.Equals () method can compare the contents of a two String.
3. Code and running results
The String.Equals () method compares the value of two strings, not the Address.
"Java" Caesar Encryption and After-class hands-on brain