Java string class constant pool analysis and detailed introduction to the differences between “equals” and “= =”
Similarities and differences between Java "equals" and "= ="
First, briefly say "equal" and "= ="
==For basic data types, the operation compares whether the values of two variables are equal,
For reference variables, it indicates whether the addresses of the two variables stored in the heap are the same,
That is, whether the contents in the stack are the same
Whether the two variables represented by the equals operation are references to the same object,
That is, whether the contents in the heap are the same.
To sum up, = = compares the addresses of two objects, while equals compares the contents of two objects.
Let's briefly introduce the string class
The string class is also called an immutable character sequence
String uses private final char value [] to store strings, that is, after a string object is created, the string content stored in this object cannot be modified. String class has a special creation method, which is to use "" double quotes to create. For example, new string ("123") actually creates two string objects. One is "123" created through "" double quotes, and the other is created through new However, they are created in different periods, one is the compilation period and the other is the run period. Java overloads the + operator on the string type. You can directly use + to connect two strings. Calling the intern () method of the string class at runtime can dynamically add objects to the string pool.
Distinguish between the two methods of creating string objects' 'and new()
String is a special wrapper class data. You can use:
Two forms are created
The first is to create a new object with new (), which will be stored in the heap. Each call creates a new object. (there are actually two, as mentioned above, but after "123" exists in the constant pool, a new "123" will not be created in the constant pool.) the second is to create a variable STR for the object reference of string class in the stack, and then use symbolic reference to find out whether there is "ABC" in the string constant pool. If not, it will be "ABC" Store it in the string constant pool and make STR point to "ABC". If there is already "ABC", make STR point to "ABC" directly.
At this time, we should pay attention to
On the one hand, the first writing method is beneficial and saves memory space At the same time, it can improve the running speed of the program to a certain extent, because the JVM will automatically decide whether it is necessary to create new objects according to the actual situation of the data in the stack. For string STR = new string ("123"); Code, all new objects are created in the heap, regardless of whether their string values are equal or not, and whether it is necessary to create new objects, which increases the burden of the program. On the other hand, we are using such as string STR = "123"; When defining a class in the format of, it is always taken for granted that the object STR of the string class is created. Object may not have been created! It may just point to an object that has been created previously. Only through the new () method can we ensure that a new object is created every time.
See the following examples
Thank you for reading, hope to help you, thank you for your support to this site!