== in the Java string


      String str1 = "abc";
      String str2 = "abc";
      String str3 = "a" + "bc";       

      System.out.println(str1 == str2);
      System.out.println(str1 == "a" + "bc");
      System.out.println(str1 == new String("abc"));      
System.out.println(new String("ab")+"c" == new String("ab")+"c");

Output
true
true
false
false

The equals (), which is generally used to compare strings is the same, is to compare the contents of the string, that is, the sequence of characters in the heap. And == is to compare whether the memory addresses of the two strings are the same (references in the heap).

The first true is easy to understand. Both str1 and str2 are in the string constant pool in memory.

String constant pool:

  • In order to reduce the number of strings created in the jvm, the string class maintains a string constant pool, which is a special storage area in the Java heap memory;
  • When creating a String object, jvm will first check the string constant pool, if the constant value of this string already exists in the pool, it will directly return the reference of the object in the pool, if it is not in the pool, it will instantiate a string And put it in the pool;
  • Constant pool: It is used to save a piece of data in the compiled class file that has been determined by java during compilation. Including constants in classes, methods, interfaces, and string constants, such as String s = "a" declaration;
The second true: It seems that "a" + "bc" is calculated, and a new object should be generated. But why are they equal? This is because "a" + "bc", a string concatenator that contains only constants, also creates constants. When they are compiled by the compiler, they are directly calculated and thrown into the constant pool. So they correspond to the same reference.
 
Three False:
The first false is obviously, the use of new means that this object will be stored on the heap. References will naturally not be the same as objects in the constant pool.
 
The second false is because the "ab" + "c" operation at runtime is actually a StringBuilder object created by the compiler and appended, and a string object returned by toString () is stored on the heap.
 
More examples:
String s0 = "111";              //pool
String s1 = new String("111");  //heap
final String s2 = "111";        //pool 
String s3 = "sss111";           //pool
String s4 = "sss" + "111";      //pool
String s5 = "sss" + s0;         //heap 
String s6 = "sss" + s1;         //heap
String s7 = "sss" + s2;         //pool
String s8 = "sss" + s0;         //heap
 
System.out.println(s3 == s4);   //true
System.out.println(s3 == s5);   //false
System.out.println(s3 == s6);   //false
System.out.println(s3 == s7);   //true
System.out.println(s5 == s6);   //false
System.out.println(s5 == s8);   //false
For final String s2 = "111", it is a variable modified with final. It is known at compile time. When the string connector "a" + s2 containing the variable directly replaces s2 with the constant "111", etc. Effective for "a" + "111", the string object "a111" object has been produced in the constant pool at compile time.


Reference:
https://www.jianshu.com/p/039d6df30fea

 
 

Guess you like

Origin www.cnblogs.com/Nullc/p/12692837.html