Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Stuart A. Burkett wrote:String.intern()
Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Saif Asif wrote:Greetings priyanaka,
If you are using the new keyword to create a string , then the JVM creates a new object in the pool
the one that represents the literal of the string , and there will be a separate String object, not in the pool, that contains a copy of the content of the pooled object.
So the intern() method is to be used whenever we want any String to be inserted into the pool ?Cambell wrote: The intern method has the advantage that it works on Strings which are not literals or compile‑time constants. You can enter a String like "Saif" at the command line or wherever and it will still be interned correctly.
String a = "123" //Is it in pool? If yes then get that , else create new object
String b = "123" //Is it in pool ? yes ? get it and DON'T create new object
Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Saif Asif wrote:
First line will check to see if the String literal is in the pool or not .
Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)