Create Java String Using ” ” or Constructor?


In Java, a string can be created in two ways:

String x = "abc";
String y = new String("abc");

What is the difference between using the double quotes and using the constructor?

1. Double Quotes vs. Constructor

This question can be answered by using two simple examples.

Example 1:

String a = "abcd";
String b = "abcd";
System.out.println(a == b);  // True
System.out.println(a.equals(b)); // True

a==b is true because a and b are referring to the same string literal in the method area. The memory references are the same.

When the same string literal is created more than once, only one copy of each distinct string value is stored. This is called "string interning". All compile-time constant strings in Java are automatically interned.

Example 2:

String c = new String("abcd");
String d = new String("abcd");
System.out.println(c == d);  // False
System.out.println(c.equals(d)); // True

c==d is false because c and d refer to two different objects in the heap. Different objects always have different memory references.

This diagram illustrate the two situations above:
constructor vs double quotes Java String - New Page

2. Run-Time String Interning

Thanks to LukasEder (his comment below):

String interning can still be done at run-time, even if two strings are constructed with constructors:

String c = new String("abcd").intern();
String d = new String("abcd").intern();
System.out.println(c == d);  // Now true
System.out.println(c.equals(d)); // True

3. When to Use Which

Because the literal "abcd" is already of type String, using constructor will create an extra unnecessary object. Therefore, double quotes should be used if you just need to create a String.

If you do need to create a new object in the heap, constructor should be used. Here is a use case.

Category >> Basics >> Diagram >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. LukasEder on 2014-3-4

    When the same string literal is created more than once, JVM store only one copy of each distinct string value. This is called “string interning“.

    That’s not entirely correct. In your example, the compiler will already make sure that the same string constant is effectively referenced. String interning can still be done at runtime, even if two strings are constructed with constructors:

    String c = new String(“abcd”).intern();
    String d = new String(“abcd”).intern();
    System.out.println(c == d); // Now true
    System.out.println(c.equals(d)); // True

  2. Feng Sun on 2014-3-19

    good to know, thanks

  3. 智 陶 on 2014-3-30

    String c = new String(“abcd”).intern();
    String d = new String(“abcd”).intern();
    System.out.println(c == d); // True in JDK1.7 while false in JDK 1.6.
    correct me if i am wrong

  4. Charly on 2014-4-8

    You are worng, In JDK1.6 and JDK1.7 return true. Test rourself

  5. Ritter Liu on 2014-4-28

    Thank you for your share 😛

  6. Walt Corey on 2014-6-7

    This is how one writes code nobody can understand. Good way to be top of the list come next layoff. I knew a guy did the same crap wit C++, so ‘highly optimized” nobody could read it, and it also didn’t work but broke in mysterious ways. Rather than seeing how obtuse you can code, see how readable you can. Readability is FAR more cost effective than so-called highly optimized code.

  7. affan on 2015-1-22

    Sir I am newly joined Java programming certificate course my preceptor give me an project & I am stack on it for instance sir kindly advice me that how could I will manage program where I using string,double,integer variable in simultaneously please give some example.

  8. sss on 2015-3-6

    in jdk 1.6 is also true

Leave a comment

*