praveena satish wrote:What does interning mean and why is it required
Yeah, it would probably be a good idea to elaborate what you are asking here. What "interning" are you referring to here? Are you asking about the intern() method of the String class?
In the above program, seeing the first line an new instance is created in heap,but not in SCP.
in the second line, the intern method creates an instance in the SCP but returns the same reference s;
so if add few more lines of code,
so here should we understand it as the reference s is pointing to both the instances one in heap another one is SCP?
No, that doesn't look right. The String#intern() method returns
praveena satish wrote:. . . the intern method creates an instance in the SCP but returns the same reference s . . .
So all interned Strings are in the “pool”. The first String put into that pool is the literal "helloworld" which is entered into the pool whenever that class is loaded. Then the two intern() calls find a String with “the same contents” alread in the pool, and therefore return a reference to that String. Then you are testing all three references in the pool (not on the ordinary parts of the heap). I think the Strings remaining in the ordinary part of the heap may now be eligible for GC if there are no other references to them, but I am not certain.
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
Your three uses of the same‑object operator (==) simply demonstrate the transitive property of the equivalence relation represented by Object#equals().
I looked through the API index and there appears only to be one intern() method in the whole standard API.
You don't need to know this for ordinary programming; I think this discussion would fit better in something like the OCJP forum.