The character of string in Java, the difference between a literal object and a construction method

Source: Internet
Author: User

Java supports two common ways to create strings

string S1 = new String ("abc"); String s2 = "abc";

What's the difference between the two methods? The memory graph is as follows

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/78/BF/wKiom1aCeXWR6kguAAFwKLLRhwQ083.jpg "title=" Memory graph. jpg "alt=" wkiom1acexwr6kguaafwkllrhwq083.jpg "/>

1,S1 first looks at whether string ABC exists in the string constant pool, and if it exists, it points directly to it, and does not exist, creating a

2,S2 See if ABC exists in the constant pool and the result already exists, point directly to the reference


Difference

The former S1 creates an ABC of the new string and the string constant area in two object heap memory

S2 only created an object ABC if there is ABC, then an object is not created.



At the same time String s = "abc";

String ABC as an object can also call methods of the String class

This article is from the "Homecoming" blog, make sure to keep this source http://firrty.blog.51cto.com/4028329/1729726

The character of string in Java, the difference between a literal object and a construction method

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.