In-depth explanation of Java String string

There are two forms of string object creation in Java, one is literal form, such as String str = "hello";, the other is to use the standard method of constructing new objects, such as String str = new String(" hello");

For such common sense, I will not repeat it.

First of all, the String class is a final class, why is it defined as final?

To put it simply, for such a frequently used data type, the designers think that the design is good enough and does not need to be inherited, otherwise random inheritance and rewriting may reduce the performance of the program.

As mentioned in the title, since it is in-depth, let's dig down the little actions of String at the jvm level.

First explain the form of literal creation:

When a literal form appears in the code to create a string object (official website: www.fhadmin.org), the JVM will first check the literal. If there is a reference to a string object with the same content in the string constant pool, it will This reference is returned, otherwise a new string object is created, then this reference is put into the string constant pool, and the reference is returned.

As follows:

String str1 = "hello" ;

When we first created it, here we thought that no object with the content hello existed. If the JVM cannot find the string object with the content hello through the string constant pool, it will create the string object, and then put the reference of the newly created object into the string constant pool, and return the reference to the variable str1

If there is a code like this next

String str2 = "hello" ;

Similarly, the JVM still needs to detect this literal. The JVM searches the string constant pool and finds that the "hello" string object exists, so it returns the reference to the existing string object to the variable str2. Note that new string objects are not recreated here .

To verify whether str1 and str2 point to the same object, we can pass this code

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

The result is true.

The second is created using new:

String str3 = new String("hello");

When we use new to construct a string object, a new string object will be created regardless of whether there are references to objects with the same content in the string constant pool. So let's test it with the following code,

String str3 = new String("hello");
System.out.println(str1 == str3);

The result is false. Indicates that the two references point to different objects.

intern

For the string object created using new above, if you want to add a reference to this object to the string constant pool, you can use the intern method.

After calling intern, first check whether there is a reference to the object in the string constant pool, if it exists, return the reference to the variable, otherwise add the reference and return it to the variable.

String str4 = str3.intern();
System.out.println(str4 == str1);

The result is true.

Difficult Problems

Preconditions?

The prerequisite for the implementation of the string constant pool is that the String object in Java is immutable, which can safely ensure that multiple variables share the same object. If the String object in Java is mutable, a reference operation changes the value of the object, then other variables will also be affected, (official website: www.fhadmin.org) Obviously this is unreasonable.

reference or object

The most common question is whether to store references or objects in the string constant pool. The string constant pool stores object references, not objects. In Java, objects are created in heap memory. String constant pool exists in the permanent generation of heap memory

Advantages and disadvantages

The advantage of the string constant pool is to reduce the creation of strings with the same content and save memory space.

If you insist on the disadvantages, it is sacrificing CPU computing time for space. CPU computing time is mainly used to find whether there are references to objects with the same content in the string constant pool. However, its internal implementation is HashTable, so the computational cost is low.

GC recycling?

Because the string constant pool holds references to shared string objects, does this mean that these objects cannot be recycled?

First of all, the objects shared in the problem are generally small. As far as I have verified, this problem did exist in earlier versions, but with the introduction of weak references, this problem should be gone at present.

intern use?

The premise of using intern is that you know that you really need to use it. For example, we have a million records here, and a certain value of the record is California, USA many times. We do not want to create millions of such string objects. (Official website: www.fhadmin.org) We can use intern only keeps one copy in memory.

Are there always exceptions?

Do you know that the following code will create several string objects and store several references in the string constant pool?

String test = "a" + "b" + "c";

The answer is that only one object is created and only one reference is kept in the constant pool. We use javap to decompile and take a look to find out.

In fact, during compilation, these three literals have been combined into one. Doing this is actually an optimization, avoiding the creation of redundant string objects, and no string concatenation problems.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326941555&siteId=291194637