22

What is the difference between the following two initializations in Java?

  1. String a = new String();
  2. String b = new String("");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3575425
  • 478
  • 3
  • 11
  • 2
    Already a very similar post for this. http://stackoverflow.com/questions/9555077/string-initialization-difference – Jack Jefferies Apr 09 '15 at 10:07
  • possible duplicate of [String empty constructor in java](http://stackoverflow.com/questions/12430112/string-empty-constructor-in-java) – Joe Apr 09 '15 at 12:38
  • 1
    @Joe Although I'm shocked this doesn't exist already, I don't think that question is a duplicate of this one -- `String("")` is different from `""`. – Patrick Collins Apr 09 '15 at 21:53
  • I dont think you should use new String() either way. It does not need initialized and doing so only takes longer. Correct me if i am wrong. – NightSkyCode Apr 09 '15 at 22:38

7 Answers7

20

Well, they are almost the same.

public static void main(String[] args) {
    String s1 = new String();
    String s2 = new String(""); 
    System.out.println(s1.equals(s2)); // returns true.
}

Minor differences (rather insignificant) :

  1. new String(); takes less time to execute than new String(""); because the copy constructor does a lot of stuff.

  2. new String("") adds the empty String ("") to the String constants pool if it is not already present.

Other than this, there are no other differences

Note : The use of new String("abc") is almost always bad because you will be creating 2 Strings one on String constants pool and another on heap with the same value.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
4

Java Docs explains it beautifully

These are 2 different constructor calling

public String()

Initializes a newly created String object so that it represents an empty character sequence. Note that use of this constructor is unnecessary since Strings are immutable.

public String(String original)

Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
3

Internally, different constructors will be invoked.

However, the resulting String objects will be identical by their content and equal (a.equals(b) will return true)

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
2

In first case you create only one String object in second case two: "" and new String, if "" object not already exist in string pool.

  1. Initializes a newly created String object so that it represents an empty character sequence.

  2. Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

Community
  • 1
  • 1
Aleksandr Podkutin
  • 2,532
  • 1
  • 20
  • 31
  • 1
    Is the `""` being created at this point? or does it already exist in the interned strings pool? –  Apr 09 '15 at 18:55
  • @MichaelT Thanks for question. If you not created another `""` string object earlier it will be created. If you create `""` string object earlier, value will be got from string pool. – Aleksandr Podkutin Apr 10 '15 at 07:29
  • My reading of [this answer](http://stackoverflow.com/a/16785991/289086) suggests that the String interning is done at class load time (as its part of the constant pool for the class) - not when the method is called. Thus, no new string for `""` is created when the method is called. Just a minor correction (I think). –  Apr 10 '15 at 15:37
2

TheLostMind is mostly correct, but I'd like to add that the copy constructor doesn't actually do that much:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/lang/String.java#137

137  public String() {
138 this.value = new char[0];
139 }

151 public String(String original) {
152 this.value = original.value;
153 this.hash = original.hash;
154 }

Using the constant "" will use the first constructor to create the object reference anyway, so it doesn't matter too much which one you use.

In any case, I would recommend using the string literal "" because you can save an object reference if you use that string elsewhere. Only use the String constructor if you really need a copy of that string that isn't used anywhere else.

Wires77
  • 251
  • 1
  • 4
  • 18
  • I find it interesting that given a million-character string `foo`, the statement `bar = new string(foo);` will share the backing store, but the method call `foo.equals(bar)` will compare every character of that backing store to itself, rather than notice that both strings share the same backing store. – supercat Apr 09 '15 at 23:17
  • Well - Prior to java7u40, there was a lot going on in the copy constructor. Now, they have changed the design so that the same back-end array is used. In jdk 6 we used to create a new char array in the copy constructor :) – TheLostMind Apr 13 '15 at 05:19
1

The first is calling the default constructor and the second is calling the copy constructor in order to create a new string in each case.

1

From a purely practical point of view, there is zero difference between those constructions, as there is never any reason to ever use either of them. They are both wasteful and over-complicated, and thus equally pointless.

To initialize a variable with the empty string, do:

String s = "";

That is shorter and plainer to type, and avoids creating any String objects, since the one shared "" instance in the intern pool will certainly have already been loaded by some other class anyway.

Boann
  • 48,794
  • 16
  • 117
  • 146