Java string source code and comprehensive analysis of string constant pool
1. Introduction to string, common methods and source code analysis
2. String constant pool analysis
common method
equals
trim
replace
concat
split
Startswith and endswith
substring
Touppercase() and tolowercase()
compareTo
String introduction
The string class is modified by final, that is, the string object is non variable, and concurrent programs like non variable best. String class implements serializable, comparable and charsequence interfaces.
Start with a piece of code:
Let's guess what the result is? If your conclusion is true. Well, another piece of code:
What happened? The correct answer is false.
Let's see how the compiler compiles the code
In other words, the first code has been optimized at compile time because the compiler found that the effects of "a" + "B" + 1 and "Ab1" are the same, which are composed of non variables. But why are their memory addresses the same? If you are still interested in this, let's take a look at some important source code of the string class.
Source code
1、 String property
The string class contains an immutable char array to store the string, and an int variable hash to store the calculated hash value.
2、 String constructor
3、 String common methods
1. equals
1 first, judge whether the same object is referenced = = that is, judge whether the memory addresses of the two references are the same. If they are the same, return true directly
2 will judge whether the types are the same and whether they are the same data type
If the type is the same, it will compare whether the length of the converted character array is the same
4 compare whether each character is the same from back to front
Judgment order = 1 Memory address 2 Data type 3 Character array length 4 Single character comparison
2. compareTo
This method is cleverly written. First judge the character size from 0.
If the two objects can compare characters and are equal after comparison, they directly return their own length minus the length of the compared object. If the two strings are equal in length, they return 0, which skillfully judges the three situations.
3.hashCode
The string class overrides the hashcode method. The hashcode method in object is a native call.
The hash of string class is calculated by polynomial. We can get the same hash from different strings. Therefore, the same hashcode of two string objects does not mean that two strings are the same.
The hashcodes of the same string object must be the same, but the hashcodes are the same, not necessarily the same object
4.startsWith
Both the start comparison and the end comparison are frequently used. For example, this method can be used to judge whether a string is HTTP protocol or whether a file is MP3 file.
5.concat
Concat method is also one of the frequently used methods. It first determines whether the added string is empty to decide whether to create a new object.
1 if the spliced character length is 0, the original character object is returned directly
2. The spliced character is not empty, and a new character object is returned
Judge the character length to generate a new object
6.replace
This method also has some advantages. For example, find out the location of the old value at the beginning, which saves some comparison time.
The replace (string oldstr, string newstr) method judges by regular expressions.
7.trim
8.intern
The ntern method is a native call. Its function is to find equivalent objects in the constant pool in the method area through the equals method,
If it is not found, open up a space in the constant pool to store the string and return the reference of the corresponding string. Otherwise, directly return the reference of the existing string object in the constant pool.
You can also force the character object created for the new method to check whether the constant pool already exists
The second code in the introduction
The result is true because the address pointed to by a comes from the constant pool, while the string constant pointed to by B calls this method by default, so both a and B point to the same address space.
At jdk1 In 7, when the string class is used as the key, the hash related set class no longer uses the hashcode method to discrete data, but uses the hash32 method.
By default, this method uses the current system time, string class address, system class address, etc. as factors to calculate the hash seed. Through the hash seed, a 32-bit int value is obtained through the hash.
These are some simple common methods.
summary
String object is an immutable type. A string method with a return type of string returns a new string object every time, except for some specific conditions of some methods.
There are three ways to compare string objects:
==Memory comparison: directly compare the memory values pointed to by two references, which is accurate, concise, direct and clear.
Equals string value comparison: compares whether the literals of the object referred to by two references are equal.
Hashcode string numeric comparison: numeric string. The hashcodes of the two references are the same, which does not guarantee that the memory and literal values are the same.
Design idea of string constant pool
I Original design intention of string constant pool
Each string is a string object, which will be frequently used in system development. If it is created and destroyed like other pairs, it will greatly affect the performance of the program.
In order to improve performance and reduce memory overhead, the JVM optimizes the instantiation of strings
A string constant pool is opened for strings, similar to a cache
When creating a string constant, first judge whether the string exists in the string constant pool
If the string exists, the reference instance will be returned. If it does not exist, instantiate the string and put it into the pool
Implementation basis
The basis of this optimization is that each string constant is a constant modified by final, and there is no need to worry about data conflict in the constant pool
There is a table in the global string constant pool created by the runtime instance, which always maintains a reference for each unique string object in the pool, which means that they always refer to the objects in the string constant pool. Therefore, these strings in the constant pool will not be recycled by the garbage collector
Heap, stack, method area
To understand the string constant pool, first look at the stack method area
heap
Objects are stored, and each object contains a corresponding class
There is only one heap area in the JVM, which is shared by all threads. There are no basic types and object references in the heap, only the object itself
Objects 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, which only stores basic data type objects and user-defined object references
The data (primitive types and object references) in each stack is private
The stack is divided into three parts: basic type variable area, execution environment context and operation instruction area (storing operation instructions)
The data size and life cycle can be determined. When there is no reference to the data, the data will disappear
Method area
Static areas, like heaps, are shared by all threads
The method area contains elements that are always unique in the whole program, such as class and static variables;
String constant pool
The string constant pool exists in the method area
Code: stack method store string
Interview questions
String str4 = new string ("ABC") how many objects are created?
Split: str4 =, new string(), "ABC"
A new object can be created through new. The new method does not go to the constant pool to find out whether it already exists. As long as new, a new object will be instantiated
"ABC" each string is a string object. If it is not in the constant pool, a new object will be created and put into the constant pool. Otherwise, the object reference will be returned
Assign the object address to str4 to create a reference
Therefore, if there is no "ABC" literal in the constant pool, two objects will be created; otherwise, an object and a reference will be created
String str1 = new String("A"+"B") ; How many objects will be created? String str2 = new String("ABC") + "ABC" ; How many objects will be created?
The above comprehensive analysis of Java string source code and string constant pool is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.