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

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -