Learn about reference variables and objects from string s = new String ("abc")

Source: Internet
Author: User

One, the difference between the reference variable and the object

First, according to our instance Bean object process know, a AA; only a reference variable that declares a Class A, AA is not an object, and the object of the class is created by new.

So, String s .... Medium S is not an object, but a reference variable of type string.

Also, for string literals, the string literal itself is an object, such as "xxx", and XXX is an object.

Second, the text pool is the buffer pool (the pool of literal strings) and the string object in the heap (heap)

Because of the large use of String objects (typically objects are always allocated memory in the heap), in Java in order to save memory space and run time, for example, when comparing strings, = = is faster than Equals (), so at the compile stage, all the string literals are placed in a text pool, The text pool becomes part of the constant at run time. In a text pool, the same string constants are merged, occupying only one space.

The following two ways to verify:

1.

= = The reference address of the two objects to determine the content

String S1 = "abc" first finds in the buffer pool whether there is an ABC object, no, an ABC object is created in the buffer pool, and the reference address is assigned to S1;

String s2 = "ABC" will first find in the buffer pool whether there is an ABC object, or not create, directly with the existing ABC object, the reference address assigned to S2;

So S1 and S2 have the same reference address.

String S1 = "abc";

String s2 = "abc";

if (S1 = = s2) {

The content is output here

}

2.

When you create a string object using new, the ABC object is created in the heap regardless of whether the buffer pool and the heap already have an ABC object, and the ABC object reference address in the heap is assigned to a string reference

string S1 = new String ("abc") creates an ABC object in the heap and assigns the ABC reference to the S1

String s2 = new String ("abc") creates an ABC object in the heap and assigns the new object reference to the S2

string S1 = new String ("abc");

String s2 = new String ("abc");

if (S1 = = s2) {

No content output here

}

Third, string s = new String ("ABC") Procedure step (see--string s = "abc" and string s = new String ("abc") for a detailed article)

1) The stack opens up a space to store reference str2;
2) Create a space in the heap to store a new string object "ABC";
3) Reference str2 to the new string object "ABC" in the heap;
4) The address of the object referred to in the STR2 is the address in the heap, and the constant "ABC" address is in the pool,

Learn about reference variables and objects from string s = new String ("abc")

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.