Java string intern and literal -
are below 2 pieces of code same?
string foo = "foo"; string foo = new string("foo").intern();
they have same end result, not same (they'll produce different bytecode; new string("foo").intern()
version goes through steps, producing new string object, interning it).
two relevant quotes string#intern
:
when
intern
method invoked, if pool contains string equalstring
object determinedequals(object)
method, string pool returned. otherwise,string
object added pool , referencestring
object returned.all literal strings , string-valued constant expressions interned.
so end result same: variable referencing interned string "foo".
Comments
Post a Comment