In-depth understanding of the String class (focus)

First, I want to understand string class, look at the source code:

1 public final class String
2     implements java.io.Serializable, Comparable<String>, CharSequence {
3     /** The value is used for character storage. */
4     private final char value[];
5 
6     /** Cache the hash code for the string */
7     private int hash; // Default to 0
8     ...
9 }

As can be seen from the above

  • String class is modified final keyword means the String class can not be inherited, and its members are the default method for the final method; Once created, you can not modify the string.
  • String class implements Serializable, CharSequence, Comparable interface.
  • String instance is to achieve a value stored by the character string array.

Two, String method

Here is the String class supports methods for more details, see the  Java String API  documentation:

 Third, the string constant pool

 In the Java memory allocation, a total of three kinds of constant pool, which are Class constant pool, runtime constant pool, a string constant pool.

Allocation strings and other objects distribution, is a high need to consume time and space, and a string used very much. JVM to improve performance and reduce memory overhead, number of optimization when the instance of string: string constant pool. Whenever you create a string constant, JVM will first check the string constant pool, if the string constant pool already exists, then the reference to examples of direct returns the constant pool. If the string constant pool does not exist, examples of the string and place it in the constant pool. Since immutability String string constant pool must two identical string does not exist.

 1 public class String1 {
 2     static String a = "AB";
 3     static String b = "AB";
 4     static String c = new String("AB");
 5     static String d = "A"+"B";
 6     static String e = "A";
 7     static String f = "B";
 8     static String g = e+f;
 9     static String h = "A"+f;
10     public static void main(String[] args) {
11         System.out.println(a==b);   //true
12         System.out.println(a==c);   //false
13         System.out.println(a==d);   //true
14         System.out.println(a==g);   //false
15         System.out.println(a.equals(g));    //true
16         System.out.println(a.equals(h));    //true
17         System.out.println(a==h);   //false
18     }
19 }

Memory shown in FIG. Analysis:

Fourth, on the string concatenation operator "+"

        The "java", "language" and "Specification" these three words surface amount "+" operator to get a "javalanguagespecification" constant, and this constant directly into the string pool, this is actually an optimization, the three into one literal, avoiding the creation of excess string object. The quoted string "+" operation is performed during the Java runtime that str + str2 + str3 will be calculated during program execution, it will be re-created after a string object mosaic in the heap memory . Conclusion is: string constant "+" stitching is done at compile time, stitching string stored in the string pool; and the string references the "+" concatenation really be running , and the newly created strings stored in the heap.

For direct addition string, very efficient, because the compiler will determine its value, that is shaped like "I" + "love" + "java"; string is added, during compilation it is optimization has become "Ilovejava". For indirect addition (i.e., comprising a reference string), the form s1 + s2 + s3; low efficient than direct addition, because the compiler will not optimize the reference variable.

Five ,, About String.intern ()

intern method: a pool initially empty string, which is maintained by the class String alone. When calling intern method, if the pool already contains a string like this String object (determined by equals (oject) method), it returns the string pool. Otherwise, this String object is added to the pool, and returns the String object.

It follows the following rules: For any two strings s and t, if and only if s.equals (t) is true, s.intern () == t.intern () it is true.

The String.intern ();
then add describes that: present in the .class file constant pool, JVM is loaded during operation, and can be expanded. The String intern () method is a method to expand the constant pool; str String instance when a call intern () method, java find whether there is the same constant pool unicode string constant, if so, its reference is returned, if there is no is increased in the constant pool is equal to a unicode string str and returns its reference.

Example 1:

 1 /**
 2  * 关于String.intern()
 3  */
 4 public void test11(){
 5     String s0 = "kvill"; 
 6     String s1 = new String("kvill"); 
 7     String s2 = new String("kvill"); 
 8     System.out.println("===========test11============");
 9     System.out.println( s0 == s1 ); // to false 
10      the System. OUT .println ( " ********** " ); 
 . 11      s1.intern (); // despite the implementation s1.intern (), but does not assign its return value S1 
12 is      S2 = s2.intern (); // the reference constant pool "kvill" assigned to S2 
13 is      the System. OUT .println (S0 == S1); // flase 
14      the System. OUT .println (S0 == s1.intern ()); // to true // Description s1.intern () returns the constant pool "kvill" reference 
15      the System. OUT .println (S0 == S2); // to true 
16 }

The results: false, false, true, true.

Example 2:

 1 public class String1 {
 2      
 3     public static void main(String[] args) {
 4         /**
 5          *      String a = "AB";
 6          *      String b = "AB";
 7          *      String c = new String("AB");
 8          *      String d = "A"+"B";
 9          *      String e = "A";
10          *      String f = "B";
11          *      String g = e+f;
12          *      String h = "A"+f;
13          *         System.out.println(a==b);   //true
14          *         System.out.println(a==c);   //false
15          *         System.out.println(a==d);   //true
16          *         System.out.println(a==g);   //false
17          *         System.out.println(a.equals(g));    //true
18          *         System.out.println(a.equals(h));    //true
19          *         System.out.println(a==h);   //false
20          */
21             String s1 = "AB";
22             String s2 = new String("AB");
23             String s3 = "A";
24             String s4 = "B";
25             String s5 = "A" + "B";
26             String s6 = s3 + s4;
27             System.out.println(s1 == s2);       //false
28             System.out.println(s1 == s5);       //true
29             System.out.println(s1 == s6);       //false
30             System.OUT .println (s1 == s6.intern ());       // to true 
31              System. OUT .println (s2 == s2.intern ());       // false I understand: left s2 = new String ( "AB" );
 32                                                          // the right s2.intern () and String s2 = "AB" is meant a,
 33                                                          // so unequal sides 
34          }
 35 }

Memory analysis:

 

 Great God Reference: https://blog.csdn.net/ifwinds/article/details/80849184

                   https://www.cnblogs.com/xiaoxi/p/6036701.html

             Which is more detailed and want to understand more deeply, you can click on the link! !

 

Guess you like

Origin www.cnblogs.com/qiaoxin11/p/12551441.html