String.intern () using summary

First Blood

Look at the following code:

String s = new String("1");
String s1 = s.intern();
System.out.println(s == s1);
复制代码
打印结果为:
false
复制代码

For new String("1"), two objects are created, a type String object, it is stored in the Java Heap , the other is a constant string object "1", it is stored in a string constant pool of. s.intern()The method will first go to the pool to find out whether a string constant string constant object exists "1" if there is an address of the object is returned, if it does not exist in the raw string constant pool to become a "1" string constant object and returns the address of the object. As shown below:

image.png
Variable s points to the type of object Stirng variable s1 points to the "1" constant string objects, the s == s1 result is false.

Double kill

On the basis of the above we then define a s2 follows:

String s = new String("1");
String s1 = s.intern();
String s2 = "1";
System.out.println(s == s1);
System.out.println(s1 == s2); // true
复制代码

s1 == s2Is true, that is directly to the variable s2 string constant, as shown below:

image.png

Triple kill

On the basis of the above on a t we then defined as follows:

String s = new String("1");
String t = new String("1");
String s1 = s.intern();
String s2 = "1";
System.out.println(s == s1);
System.out.println(s1 == s2);
System.out.println(s == t);   // false
System.out.println(s.intern() == t.intern());   // true
复制代码

s == tIs false, it is clear that the variable t and the variable s is pointing to two different objects of type String. s.intern() == t.intern()It is true, because the same intern method returns a string constant pool "1" object, is true.

image.png

Ultra kill

On the basis of the above we then define a s3 and x are as follows:

String s = new String("1");
String t = new String("1");
String x = new String("1") + new String("1");
String s1 = s.intern();
String s2 = "1";
String s3 = "11";
System.out.println(s == s1);
System.out.println(s1 == s2);
System.out.println(s == t);
System.out.println(s.intern() == t.intern());
System.out.println(x == s3);  // fasle
System.out.println(x.intern() == s3.intern());  // true
复制代码

Variable x is two objects of type String added, because x != s3, it is certainly not pointing to x string constants, x is in fact an object of type String, call the x.intern()method will return "11" corresponding string constant, it x.intern() == s3.intern()is true .

Rampage

The above code to simplify and add a few variables as follows:

String x = new String("1") + new String("1");
String x1 = new String("1") + "1";
String x2 = "1" + "1";
String s3 = "11";

System.out.println(x == s3);  // false
System.out.println(x1 == s3);  // false
System.out.println(x2 == s3); // true
复制代码

x == s3False to point to objects of type String x, s3 to a string constant; x1 == s3false to point to objects of type String x1, s3 to a string constant; x2 == s3true to x2 to a string constant, s3 to a string constant;

So we can see that new String("1") + "1"the object of type String returned.

to sum up

Now we know intern method is to save string to the constant pool, in the process to save the string constant pool will first check whether the same string constant pool already exists and, if the string is used directly. So when we write business code, we should try to use the string string constant pool, such as the use String s = "1";than the use of new String("1");more economical memory. We can also use the String s  = 一个String类型的对象.intern();method to use indirect string constants, such practices are used when you received an object of type String and want to save memory in the case, of course, you can String s = an object of type String; but so with a reference variable s may be because of the impact garbage collection objects of type String. So we can use the method to optimize the intern, but note that interncan save memory, but will affect the speed, because the method needs to find out if there is a string constant pool.

If you find this article allows you to acquire knowledge, you can help forward , knowledge sharing out. If you want the first time to learn more exciting content, please focus on micro-channel public number: 1:25

Guess you like

Origin juejin.im/post/5d0ccfe6e51d45773e418a92