In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "where is the Java String string constant pool". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "where is the Java String string constant pool?"
The Design idea of string constant Pool
String allocation, like other object allocation, consumes a high cost of time and space. As the most basic data type, a large number of frequent strings are created, which greatly affects the performance of the program. In order to improve performance and reduce memory overhead, JVM makes some optimizations when instantiating string constants to open up a string constant pool for strings, similar to when creating string constants in cache. First of all, insist on whether the string exists in the string constant pool and return the reference instance, which does not exist. The basis for instantiating the string and putting it into the pool is that the optimization is based on the fact that the string is immutable. There is a table in the global string constant pool created by the runtime instance without worrying about data conflicts. Always maintain a reference for each unique string object in the pool, which means that they always reference objects in the string constant pool, so these strings in the constant pool are not reclaimed by the garbage collector
Code: get the corresponding string from the string constant pool
String str1 = "hello"; String str2 = "hello"; System.out.printl ("str1 = = str2": str1 = = str2) / / true
Where is the string constant pool?
When analyzing the location of the string constant pool, first take a look at the heap, stack, and method area:
Heap
Objects are stored, and each object contains a corresponding class JVM. Only one heap is shared by all threads. Basic types and object references are not stored in the heap. Only objects stored in the object are collected by the garbage collector, so the size and life cycle do not need to be determined.
Stack
Each thread contains a stack area, the stack only holds references to objects of the basic data type and custom objects (not objects). The data in each stack (primitive type and object reference) is private. The stack is divided into three parts: basic type variable area, execution environment context, operation instruction area (store operation instructions) data size and life cycle can be determined, when no reference points to data. This data will disappear automatically.
Method area
The static zone, like the heap, is shared by all threads in the method zone that contains elements that are always unique throughout the program, such as the class,static variable
The string constant pool exists in the method area.
Code: stack method area stores strings
String str1 = "abc"; String str2 = "abc"; String str3 = "abc"; String str4 = new String ("abc"); String str5 = new String ("abc")
Creation of string object
Interview question: String str4 = new String ("abc") how many objects are created?
Look for a "abc" object in the constant pool
If the corresponding reference instance is returned, the corresponding instance object will be created if not.
New a String ("abc") object in the heap to assign the object address to str4, creating a reference
So, if there is no "abc" literal in the constant pool, create two objects, otherwise create an object and create a reference
According to the literal quantity, the following variant questions are often raised:
String str1 = new String ("A" + "B"); how many objects will be created? String str2 = new String ("ABC") + "ABC"; how many objects will be created?
Str1: string constant pool: "A", "B", "AB": 3 heaps: new String ("AB"): 1 reference: str1: 1 total: 5
Str2: string constant pool: "ABC": 1 heap: new String ("ABC"): 1 reference: str2: 1 total: 3
Code: variables and constants of the underlying type, variables and references are stored in the stack, and constants are stored in the constant pool
Int A1 = 1 int a2 = 1 politics int a3 = 1 x public static int INT1 = 1 x public static int INT2 = 1 x public static int INT3 = 1
The way to manipulate the string constant pool
When JVM instantiates a string constant pool
String str1 = "hello"; String str2 = "hello"; System.out.printl ("str1 = = str2": str1 = = str2) / / true
String.intern ()
String objects created by the new operator do not point to any objects in the string pool, but you can point to one of them by using the string's intern () method. Java.lang.String.intern () returns a reserved pool string, that is, an entry in the global string pool. If it is not previously in the global string pool, it will be added to it
/ / Create three strings in three different ways. String S1 = "Hello"; String S2 = new StringBuffer ("He"). Append ("llo"). ToString (); String S3 = s2.intern (); / / Determine which strings are equivalent using the = = / / operator System.out.println ("S1 = = S2?" + (S1 = = S2)); / / false System.out.println ("S1 = = S3?" + (S1 = = S3); / / true
Supplement: a preliminary study of literal quantity and constant pool
String objects are stored internally using an array of characters, so take a look at the following example:
String m = "hello,world"; String n = "hello,world"; String u = new String (m); String v = new String ("hello,world")
An 11-length char array is assigned, and a string consisting of the char array is assigned in the constant pool, and then m references the string and uses n to reference the string in the constant pool, so the same object as n references a new string, but the internal character array references the character array inside m to generate a new string. But the internal character array refers to the character array inside the string in the constant pool, which means that u is the same character array as u.
If you use a graph to represent it, it looks something like this (using dotted lines only means that there is no special relationship between the two):
Test demo:
String m = "hello,world"; String n = "hello,world"; String u = new String (m); String v = new String ("hello,world"); System.out.println (m = = n); / / true System.out.println (m = = u); / / false System.out.println (m = = v); / / false System.out.println (u = v); / / false
At this point, I believe you have a deeper understanding of "where is the Java String string constant pool". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.