Several problems of String class in Java

Source: Internet
Author: User

First, we need to figure out that in Java, references and basic data types are stored in the stack. The objects are stored in the heap.

There is only one exception, the string object.

For example:

String str1= "Test"; String str2= "Test"; String Str3=new string ("Test1"); String Str4=new string ("Test1"); System.out.println (STR1==STR2); System.out.println (STR3==STR4);

For STR1 and str2, the object "test" they refer to is neither in the stack nor in the heap. Instead, it is stored in a string pool.

There is only one "test" in the string, which means that no matter how many str= "test" you declare, only one copy is stored in the string pool, and all str points to the same address.

And the situation of STR3,STR4 is not the same.

STR3 and STR4 Use the new keyword to generate objects, and objects generated by the new keyword are stored in the heap, so str3 and STR4 are pointing to two different objects.

Therefore, the output of the above program is

True

False

Reference: http://sarin.iteye.com/blog/603684/

Several problems of String class in Java

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.