Possible Duplicates:
Java String declaration
Java Strings: “String s = new String(”silly“);”
What is the purpose of the expression “new String(…)” in Java?
Whats is the difference between
String a = new String("SomeValue");
and
String a = "SomeValue";
What is the difference and Which one is better and why ?
Thanks.
Unless you have a unusual, specific need and use case, always use the 2nd version, without the new.
Edited in response to @Ynwa
If you specifically need a String that you know is unique, and you will be comparing with == (which is also unusual), then use the 1st case. For example, it you have some Queue of Strings, and you need a specific String to mean “all done”. Now, conceivably, you could use null or some weird String of Armenian characters, but maybe null is legal for your logic, and what if your software eventually gets used in Armenia? The clean way is
If the client puts “Terminator” on the Queue, it will get processed. So you do not prevent them from using “Terminator”. But if the client puts ThatQueueClass.TERMINATOR onto the Queue, it will get shut down.