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