Possible Duplicate:
difference between string object and string literal
When initializing a String object there are at least two ways, like such:
String s = "some string";
String s = new String("some string");
What’s the difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The Java language has special handling for strings; a string literal automatically becomes a
Stringobject.So in the first case, you’re initializing the reference
sto thatStringobject.In the second case, you’re creating a new
Stringobject, passing in a reference to the originalStringobject as a constructor parameter. In other words, you’re creating a copy. The referencesis then initialized to refer to that copy.