What's the difference between String and new String? [duplicate]
Possible Duplicate:
What is the purpose of the expression “new String(…)” in Java?
What's the diffe开发者_如何学Pythonrence 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 String
by calling substring()
and then discarding the original, you can save memory by using this constructor to create new String
s from the sub-strings. That's really an obscure case, though.)
精彩评论