In SCJP(v 1.4) book by KathySierra chapter no. 6 Java.lang -The Math class,Strings, and Wrappers :
For the statement at line 1 ,its represented diagramatically that a String object with value "abc" gets created on heap and refers s to it.In the statement at line 1 "new String()" is not used then how come String object gets created on heap.I feel that it just creates the String object in String Constant Pool if compiler doesn't find the literal "abc" in String Constant Pool.Where is this String Constant pool? whether this pool is a part of heap or is totally a different thing and is located somewhere else?
With this confusion my String basic concepts are getting disturbed.Pls explain???
I read, Corey's blog in this forum. It is clearly stated that all the objects in Java are created on the heap and String are not exceptions. When you write:
An object is created on the heap and the String constant pool stores its reference. Ref variable s is assigned the reference of the String object created on the heap.
Next line when we write again like: 2- String s2 = "abc"; The String constant pool is checked whether a reference to String literal "abc" already exists; if yes the pool returns the reference of the same object just back created on the heap.
That is why when we compare two objects using
(s1==s2) // true
I like if somebody says" String Constant Pool is pool of String references of objects created on the heap. Is that correct? Although it composed is my wordings!