3

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.

Community
  • 1
  • 1
nik7
  • 3,018
  • 6
  • 25
  • 36

2 Answers2

5

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

    public final static String TERMINATOR = new String("Terminator");  // actual text doesn't matter ... 
   // then, some loop taking from the Queue 
   while (keepGoing) {    
      String s = myQueue.take();    
      if (s == TERMINATOR) 
         keepGoing = false;    
     else   
       // normal processing of s 
   }

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.

user949300
  • 15,364
  • 7
  • 35
  • 66
  • are there any instances where we need the first case ? – nik7 Jan 21 '12 at 23:25
  • @ynwa Yes; if you specifically need a new String instance. – Dave Newton Jan 21 '12 at 23:26
  • The second one is better for most purposes, because it implicitly instantiates and initializes a String object with value "SomeValue". String a = "SomeValue"; The first method is rarely used except to create an independent copy of an existing string variable. String b = new String(a); Because `String b = a` just creates a copy of a reference to the same object, i.e. both `a` and `b` will be pointing to the same object! – Web User Jan 21 '12 at 23:26
  • @ynwa: If you're working with native code, then maybe. – user541686 Jan 21 '12 at 23:26
4

In java there is a concept of String literal pool.To cut down the number of String objects created in the JVM, the String class keeps a pool of strings. Each time your code create a string literal, the JVM checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool.

 String str1 = "Hello";  
 String str2 = "Hello"; 
 System.out.print(str1 == str2);

Prints True.

If you do :

String str1 = "Hello";  
String str2 = new String("Hello");
System.out.print(str1 == str2);

Prints False.

because, String object is created out of the String literal pool.

RanRag
  • 48,359
  • 38
  • 114
  • 167