String constant pool and heap String

Constant pool:
       Constant pool (constant pool) refers is determined at compile time, and saved some data compiled .class file. It includes constant about classes, methods, interfaces, and the like, including string constants.

       In the constant storage pool string constant memory space, namely a string constant pool when the need to use the string, the string go to the pool to see if the string already exists, if present, can be used directly, if there is no , initialization, and the string in the string constant pool.

       Reflections from the string angle: references are stored on the stack, if it is already compile created (direct assignment string) is stored in the constant pool of its target, if it is running on (new out) can be determined stored on the heap. For strings are equal equals, always only in a constant pool, multiple copies of the heap.

 

Use String direct assignment:
String str = "abc": might create or not create a target. If the "abc" does not exist in the string pool, the pool will be created in java string a String object ( "abc"), then str point to this memory address, the number is "abc" No matter created in the future in this way string object, always only one memory address is assigned; if "abc" exists in the string pool, str directly to this memory address.

Example:

String str = "abc";

String str1 = "abc";

String str2 = "abc";

System.out.println(str==str1);//true

System.out.println(str==str2);//true

 

Create a string using the String new new:
String str = new new String ( "abc"): will create at least one object, it is also possible to create two. Because the new keyword is used, will definitely create a String object on the heap, if the characters in the pool already exists "abc", it does not create a String object in the string pool, if not present, will be in a string constant pool also create an object.

Example:

String str = new String("abc");

String str1 = new String("abc");

String str2 = new String("abc");

System.out.println(str==str1);//false

System.out.println(str==str2);//false

 

String string concatenation:
In addition to the direct use = Assignment, also used string concatenation, string concatenation is divided into variable stitching and known string concatenation.

As long as there is spliced ​​content variable, the new variable after the stitching is new in a heap memory object entity.

Example:

String str = "abc"; // create abc in the constant pool

String str1 = "abcd"; // create abcd in the constant pool

String str2 = str + "d"; // string concatenation, in this case will be a new stack object abcd, since the compiler is not known before str2

String str3 = "abc" + "d"; // after splicing str3 or abcd, it will still point to the memory address of the string constant pool

System.out.println(str1==str2);//false

System.out.println(str1==str3);//true

 

String.intern ():
        when calling intern method, if the constant pool already contains a string like this String object (determined by equals (Object) method), then returns the string pool. Otherwise, this String object is added to the constant pool, and returns the String object.

Example:

String s1=new String("abc");

String s2=s1.intern();

System.out.println( s1==s2 ); //false

System.out.println( s1+" "+s2 ); // abc abc

System.out.println( s2==s1.intern() ); //true

s1 == s1.intern () is false description of the original "abc" still exists;

 

to sum up:  

   1.String str = "abc" and String str = new String ( "abc "); produce several objects?
          The former 1 or 0, 1 or 2 which is, look at the string constant pool, if the string is not constant pool, is created in a constant pool, if any, direct reference to the former, the latter needs to be created in the heap memory a "abc" instance of an object.

      2. For the type of variable and constant basis: reference variables and the stack, the constant in the constant stored in the storage pool.

      3. In order to enhance the jvm (JAVA virtual machine) performance and reduce memory overhead, avoid duplication of projects created characters or do not use strings to create new String, String is best to use direct assignment.

      4. About equals () and ==: String Class for Briefly, equals () is to compare the contents of two strings are equal, returns true if equal; == is the address compare two strings are the same, i.e. whether it is a reference to the string with, if it returns true.

      5.String strings are immutable.

Guess you like

Origin www.cnblogs.com/erfsfj-dbc/p/12169325.html