java - Why is String Object Instantiation a bad practice? -


this question has answer here:

why statement bad practice :

string colour= new string("blue");  

and what's difference statement

string colour="blue";  

the first discouraged because reads string string intern pool , instantiates new object instance. wikipedia article on string interning says (in part)

in computer science, string interning method of storing 1 copy of each distinct string value, must immutable. interning strings makes string processing tasks more time- or space-efficient @ cost of requiring more time when string created or interned. distinct values stored in string intern pool.

the second example assigns reference string intern pool.


Comments