Originally posted by Sunny Jain: when you write String str = new String("javaranch");
this line will create two string object,
at the String constant Pool : "javaranch" Object will be created at the non String constanct Pool : a normal string Object
str --> refer to a String object at non constant pool --> refer to "javaranch" at the String constant pool.
this is as per my understanding.
hi sunny i read the same thing in some book but i am confused when i see intern() method of string in api document. it is given as below.
"When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned."
if new String add the value in string pool then there is no meaning for otherwise because there will be always literal value in string pool.... can you clarify this