Steve said:
I always wonder why String s = new String("test") is bad?
We always do String s = "test" instead. Why?
With the former, you'll have two distinct String objects with
the same data. You'll have the static object (initialized by
the constant string "test", and you'll have another object
created as a duplicate of that static object, and you'll have
a reference to the latter stored in "s".
With the latter, you have one static String object with data
"test", and a reference to the static object stored in "s".
The issue is mostly that of memory consumption. A reference
is small, but an object is not. The space needed for 10000
copies of String object with data "test", and 10000 references
to those objects is surely greater than the space needed for
a single object and 10000 references. Similarly, it's an issue
of efficiency; setting a reference to point to an existing object
is a lot faster than creating a new object and setting a reference
to point to that new object.