Java's string class in the stack storage mechanism

Source: Internet
Author: User
Tags format definition

The string class is a special class, the main embodiment is that it has many forms of creation, for example, string a = "abc"; Sting a=new ("abc"), the apparent results appear to be the same, but in fact the creation mechanism inside Java is very different.

For example, about the internal work of string str = "abc".
Inside Java, this statement is translated into the following steps:
(1) First define an object reference variable named str to the String class: String str;
(2) in the stack to find whether there is a value of "ABC" address, if not, then open a store with a literal "ABC" address, then create a new String Class object O, and the string value of O point to the address, and in the stack next to this address note the referenced object o. If you already have an address with a value of "ABC", look for the object o and return the address of O.
(3) point Str to the address of the object o.
It is important to note that the string values in the generic string class are directly stored values. But like string str = "abc"; In this case, the string value is a reference to the data in the existing stack!
to better illustrate this problem, we can verify it by following several code.
String str1 = "abc";
String str2 = "abc";
System.out.println (STR1==STR2);//true
Note that we do not use Str1.equals (STR2) in this way, as this will compare the values of two strings for equality. = = number, as described in the JDK, returns true only if two references point to the same object. And what we're looking at here is whether str1 and str2 all point to the same object.
The result shows that the JVM created two references str1 and str2, but only one object was created, and two references pointed to the object.
let's go further and change the above code to:
String str1 = "abc";
String str2 = "abc";
str1 = "BCD";
System.out.println (str1 + "," + str2);//BCD, ABC
System.out.println (STR1==STR2);//false
This means that the change in the assignment has led to a change in the class object reference, and str1 points to another new object! And str2 still points to the original object. In the example above, when we change the value of str1 to "BCD", the JVM discovers that there is no address for that value in the stack, opens up this address and creates a new object whose string value points to the address.
in fact, the string class is designed as an immutable (final) class. If you want to change its value, yes, but the JVM silently creates a new object at run time based on the new value, and then returns the address of the object to the reference of the original class. This creation process is entirely automatic, but it takes up more time. In the environment that is more sensitive to time requirements, it will have some adverse effects.
then modify the original code:
String str1 = "abc";
String str2 = "abc";
str1 = "BCD";
String str3 = str1;
System.out.println (STR3);//BCD
String STR4 = "BCD";
System.out.println (str1 = = STR4);//true
STR3 A reference to this object points directly to the object that str1 points to (note that STR3 does not create a new object). When str1 changes its value, it creates a reference str4 of string and points to the new object created by str1 modifying the value. It can be found that this time STR4 also did not create a new object, thereby re-sharing the data in the stack.
let's look at the following code again.
string str1 = new String ("abc");
String str2 = "abc";
System.out.println (STR1==STR2);//false
two references were created. Two objects were created. Two references point to a different two objects, respectively.
String str1 = "abc";
String str2 = new String ("abc");
System.out.println (STR1==STR2);//false
two references were created. Two objects were created. Two references point to a different two objects, respectively.
The above two code shows that as long as new () is used to create the object, it is created in the heap, and its string is stored separately, even if the data in the stack is the same, it is not shared with the data in the stack.
6. The value of the data type wrapper class cannot be modified. Not only the value of the string class cannot be modified, but all data type wrapper classes cannot change their internal values.
7. Conclusions AND recommendations:
(1) When we use a format definition class such as String str = "ABC", we always want to think of course that we created the object str of the String class. Worry about traps! The object may not have been created! The only certainty is that a reference to the string class was created. As to whether the reference is pointing to a new object, it must be considered in terms of context, unless you create a new object with a prominent way through the new () method. Therefore, it is more accurate to say that we have created a reference variable to the object of the String class str, which refers to a variable that points to a string class with the value "ABC". Being aware of this is helpful in troubleshooting bugs that are difficult to find in a program.
(2) the use of string str = "abc", in a way that can improve the speed of the program to a certain extent, because the JVM will automatically based on the actual data in the stack to determine whether it is necessary to create a new object. In the case of string str = new String ("abc"), the code creates a new object in the heap, regardless of whether the string value is equal or not, and it is necessary to create a new object, thereby aggravating the burden of the program.
(3) Use the Equals () method when comparing the values in the wrapper class, and use the = = when testing whether the references to the two wrapper classes point to the same object.
(4) because of the final nature of the string class, you should consider using the StringBuffer class when the string variable needs to change its value frequently to improve program efficiency.

Java's string class in the stack storage mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.