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 equal string object determined equals(object) method, string pool returned. otherwise, string object added pool , reference string object returned.

all literal strings , string-valued constant expressions interned.

so end result same: variable referencing interned string "foo".


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -