Possible Duplicate:
What is the purpose of the expression “new String(…)” in Java?
What’s the difference between these two statements:
String a1 = new String("abc");
and
String a2 = "abc";
If you could illustrate the difference, that would be great.
The first one is creating a new String object; the second one is effectively using one which already exists (it’s created while loading the class file.) There is virtually never a reason to use the
String(String)constructor.(I say virtually because there is one case: if you’re breaking up a huge
Stringby callingsubstring()and then discarding the original, you can save memory by using this constructor to create newStrings from the sub-strings. That’s really an obscure case, though.)