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
internmethod invoked, if pool contains string equalstringobject determinedequals(object)method, string pool returned. otherwise,stringobject added pool , referencestringobject returned.all literal strings , string-valued constant expressions interned.
so end result same: variable referencing interned string "foo".
Comments
Post a Comment