Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to do deep parsing of Java string pool String Pool

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to carry out in-depth analysis of Java string pool String Pool, the content is very detailed, interested friends can refer to, hope to be helpful to you.

In our work, the String class is an object type that we use very frequently. In order to improve performance, reduce memory overhead and avoid repeated string creation, JVM maintains a special memory space, which is the core of what we are going to discuss today, string pool (String Pool). String pooling is maintained privately by the String class.

We know that there are two ways to create a string object in Java: 1) assign a literal value 2) create a new string object using the new keyword. There are differences between the two approaches in terms of performance and memory footprint.

Method 1: assign values literally, for example:

When creating a string by literal value, JVM first goes to the string pool to see if the object "aaa" exists, if not, creates the object "aaa" in the string pool, and then returns the reference address of the object "aaa" in the pool to the string constant str, so that str points to the string object "aaa" in the pool. If it exists, no object is created, but the address of the object "aaa" in the pool is returned and assigned to the string constant.

In this example, execute: str = = str2, and you get the following result:

This is because when the string object str2 is created, the object "aaa" already exists in the string pool, and the reference address of the object "aaa" is returned directly to str2, so that str2 points to the object "aaa" in the pool, that is, str and str2 point to the same object, so the statement System.out.println (str = = str2) outputs: true.

Method 2: create a new string object using the new keyword, for example:

When creating a new string object with the new keyword, JVM first looks for the string object "aaa" in the string pool. If so, it no longer creates the "aaa" object in the pool, but directly creates a "aaa" string object in the heap, and then returns the address of the "aaa" object in the heap to the reference str3. In this way, str3 points to the "aaa" string object created in the heap. If not, first create a "aaa" string object in the string pool, then create a "aaa" string object in the heap, and then return the address of the "aaa" string object in the heap to the str3 reference, so that str3 points to the "aaa" string object created in the heap.

In this example, execute: str3 = = str4 to get the following result:

Because, when you create an object with the new keyword, each time new comes out a new object, that is, references to str3 and str4 point to two different objects, so the statement System.out.println (str3 = = str4) outputs: false.

The implementation of string pooling has a prerequisite: the String object is immutable. Because this ensures that multiple references can point to the same object in the string pool. If the string is mutable, it is obviously unreasonable that one reference operation changes the value of the object and affects other references.

Advantages and disadvantages of string pool: the advantage of string pool is that it avoids the creation of strings with the same content, saves memory, saves the time of creating the same string, and improves performance; on the other hand, the disadvantage of string pool is that it sacrifices the time required for JVM to traverse objects in constant pool, but its time cost is relatively low.

The intern method uses: an initially empty string pool, which is maintained by the class String alone. When the intern method is called, if the pool already contains a string equal to this String object (determined by the equals (oject) method), the string in the pool is returned. Otherwise, add the String object to the pool and return a reference to the String object. For any two strings s and t, s.instan () = = t.instan is true if and only if s.equals (t) is true. All literal strings and string assignment constant expressions are manipulated using the intern method.

GC collection: shared string objects are maintained in the string pool and these strings are not collected by the garbage collector.

The Java language specification (Java Language Specification) describes strings as follows: each string constant is a reference to an instance of a string class. The string object has a fixed value. String constants, or generally speaking, strings in constant expressions are reserved using the method String.intern to share unique instances. The above is the original text of the Java language specification, which is more official and translated in a more easy-to-understand language. It mainly illustrates three points: 1) each string constant points to a string instance in the string pool or heap memory; 2) the string object value is fixed and cannot be modified once created. 3) string constants or strings in constant expressions are used by the method String.intern () to retain a unique instance in the string pool. And the test program is given as follows:

Compilation unit:

Output:

This example illustrates six points:

References to string constants in the same class under the same package point to the same string object

References to string constants in different classes under the same package point to the same string object

References to string constants in different classes under different packages still point to the same string object.

Strings evaluated by constant expressions are evaluated at compile time and are then treated as constants

Strings calculated by concatenation at run time are newly created, so they are different

The calculated string shows that the result of calling the intern method is the same as the original string constant of the same content.

As can be seen from the above example, the execution process of string constants calculated at compile time is different from that calculated at run time, and the results are also different. Let's look at the following code:

The code output is as follows:

Why did the above results appear? This is because the string literal concatenation operation is performed during the compilation of the Java compiler, that is, when the compiler compiles, it directly performs the "java", "language" and "specification" literals to get a "javalanguagespecification" constant, and puts the constant directly into the string pool, which is actually an optimization, combining the three literals into one. Avoid creating redundant string objects. The "+" operation of the string reference is performed during the Java run, that is, str + str2 + str3 is calculated during the program execution, and it recreates a spliced string object in the heap memory. To sum up: the literal "+" stitching occurs during compilation, and the stitched strings are stored in the string pool, while the "+" stitching operations of string references are actually performed at runtime, and the newly created strings are stored in the heap.

Summary: a string is a constant, and each string object in the string pool has only a unique copy, which can be pointed to by multiple references, avoiding repeated creation of strings with the same content; string objects created by literal assignment are stored in the string pool, and string objects created by keyword new are stored in the heap.

This is the end of the in-depth parsing of Java string pool String Pool. I hope the above content can be of some help and can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report