Java heap, Stack, string

Source: Internet
Author: User

public class StringDemo{    private static final String MESSAGE= "taobao" ;    public static void main(String [] args) {      String a = "tao" + "bao" ;      String b= "tao" ;      String c= "bao" ;      System.out.println(a==MESSAGE);      System.out.println((b+c)==MESSAGE);    } }

For this problem, we examine the understanding of string types and compiler optimizations. String in Java is not a basic type, but sometimes it is similar to a basic type, such as String B = "Tao"; You can assign a value directly to a variable instead of a new object (you can also use new, of course). So this type of string deserves a good look.

The values of variables and basic types in Java are stored in the stack memory, and the new object itself is stored in heap memory, the reference to the object, or the stack memory. For example, the following code:

int i=1;

string s = new string ("Hello World");

The variables I and S and 1 are stored in the stack memory, while the object "Hello World" pointed to by S is stored in heap memory.

A feature of stack memory is data sharing, which is designed to reduce memory consumption, the previous definition of i=1,i and 1 are in the stack memory, if you define a j=1, then put J into the stack memory, and then look for the stack memory is 1, if there is a J point 1. If you assign a value of 2 to J, find out if there is 2 in the stack memory, and if not, put a 2 in the stack memory, then J points to 2. That is, if the constant is in the stack memory, the variable points to that constant, and if it does not add a constant to the stack's memory and points the variable to that constant.

If J + +, the variable pointed at this time does not change, but instead looks for a new constant in the stack (1 larger than the original constant), if the stack is pointing to it, if not, add the constant in the stack memory and point J to it. The comparison between this basic type and the size we logically judge is the same. If both the definition I and J are both assigned 1, the i==j result is true. = = is used to determine whether the two variables point to the same address. I==j is that I point to the 1 and J point 1 is the same? Of course it is. String constants that are directly assigned, such as String s= "Hello World", are also stored in the stack memory, and the new string object (that is, the string object) is stored in heap memory. If you define string s= "Hello World" and string w= "Hello World", s==w? Must be true because they are pointing to the same hello world.

Heap memory does not have the characteristics of data sharing, previously defined by string s = new string ("Hello World"), and after the variable s in the stack memory, Hello world, this string object is inside the heap memory. If you define string w = new String ("Hello World"), a new string object is created in heap memory, the variable w is stored in the stack memory, and W points to the new string object. Comparison of different objects in heap memory (referring to different objects of the same type) if you use = = Then the result is definitely false, like s==w? Of course, S and W point to different string objects in the heap memory. What if two string objects are judged to be equal? Use the Equals method.

Said so much just said that the problem of bedding knowledge, has not entered the subject, the following analysis of the problem. The MESSAGE member variable and the string constant it points to are definitely in the stack memory, and the variable A is also pointing to a string "Taobao"? Is it the same one? This involves compiler optimization issues. For the addition of string constants, the strings are merged directly at compile time, rather than waiting for the runtime to merge. Other words

String a = "Tao" + "Bao", and string a = "Taobao"; the compiled bytecode is the same. So wait until run time, according to the above stack memory is the data sharing principle, a and message point to the same string. And what is the case for the back (B+C)? B+c can only wait until run time to determine what string, the compiler will not optimize, think this is also reasonable, the compiler is afraid of your B value changes, so the compiler will not be optimized. Is there a "Taobao" in the "Taobao" and stack memory that B+c calculated at run time? No. The "Taobao" computed by B+c should be a string object placed in the heap memory. This can be done through system. out. println ((b+c) = = MESSAGE ); The result is false to prove it. If the computed B+c is also in the stack memory, the result should be true. Java's addition to the string is implemented through StringBuffer, first constructs a stringbuffer inside the "Tao", then calls the Append () method to append "Bao", then the value "Taobao" StringBuffer into a String object. The StringBuffer object is in heap memory, and the converted string object is rightfully in heap memory. Let's change the statement system below. out. println ((b+c). Intern () = = MESSAGE ); The result is true, the Intern () method first checks for the existence of the same string constant in the string pool (or stack memory) if there is will return. So intern () returns the "Taobao" that the MESSAGE points to. And then change the definition of variables B and C,

Final String b = "Tao";

Final String c = "Bao";

System. out. println ((b+c) = = MESSAGE );

Now B and C cannot be assigned again, so the compiler compiles b+c into "Taobao". Therefore, the result at this point is true.

In the addition of strings, as long as there is a non-final type of variable, the compiler will not be optimized, because such variables may change, so the compiler is not likely to replace such a variable with a constant. For example, the final of variable B is removed, and the result becomes false. This also means that the StringBuffer object is used, and the result of the calculation is in heap memory.

What happens if you call Intern () on a string variable that points to an object in heap memory? In fact, the question has already been said, (B+c). Intern (), b+c the result is in heap memory. It doesn't make much sense to call intern () on a variable that points to a string constant in the stack's memory. It looks for the same string in the string pool based on the value of the object in the heap memory, and points the variable to the variable in the string pool if there is one.

String a = "Tao" + "Bao";

String b = new String ("Taobao");

System.out.println (A==message); True

System.out.println (B==message); False

b = B.intern ();

System.out.println (B==message); True

System.   out. println (A==a.intern ()); True

Java heap, Stack, string

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.