If I get a string object from a method call or from StringBuilder.toString()
, does this string gets added to the String pool?
Does String get added to the pool only during class load?
If I get a string object from a method call or from StringBuilder.toString()
, does this string gets added to the String pool?
Does String get added to the pool only during class load?
I get a string object from a method call or from StringBuilder.toString(), does this string gets added to the String pool?
StringBuilder.toString()
--> (creates and ) returns a String which is on the heap (not on the String constants pool).
if you do return "hi"
in your method, then the String constant pool's hi's reference will be returned.
Strings can be added to the String constants pool during runtime by calling intern() on them.
you can call intern()
on a String
object. This will put the String object in the pool if it is not already there, and return the reference to the pooled string. (If it was already in the pool, it just returns a reference to the object that was already there).