Java BASICS (2)

Source: Internet
Author: User
1 String a="a";2 String b="b";3 String c="ab";4 String d="ab";5 String e=a+b;

The memory used to store data in the program is divided into four
1. Global zone (static Zone) (static)
2. Text Constant Area: constant strings are placed in this area, which is the constant pool we often talk about.
3. STACK: stores function parameter values and local variable values.
4. Heap: stores objects.

When we define a string
String A = "";
A is in the stack, and "A" is a String constant in the constant pool.
String B = "B ";
B is in the stack, and B is in the constant pool.
String c = "AB ";
C is in the stack, and "AB" is in the constant pool.
String d = "AB ";
D. In the stack, "AB" already exists in the constant pool, so you can directly use the existing "AB"
So at this time, both C and D point to the same "AB" in the constant pool"
String E = A + B;
E in the stack area, A + B actually generates a new String object. Since it is a string object, the result "AB" is placed in the stack area, that is, E points to the "AB" in the heap"
In this case, c = D is true, c = e is false

In addition, if you define a String object
String str1 = new string ("AB ");
Str1 is in the stack area, and the created "AB" String object is in the heap Area
String str2 = new string ("AB ");
Str2 is in the stack area, and a new "AB" object is also in the heap area, but it is not the same as the "AB" just now.
It is equivalent to having two string objects in the heap area, but the content is exactly "AB.
Therefore, str1 = str2 is false.

The constant string placed in the constant pool can be reused, but it must be the string you directly use. Although the result obtained in the form of a + B is "AB ", but it is not the use of the String constant "AB"

 

 

 

 

 

 

 

Related Article

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.