user3575425
user3575425

Reputation: 478

What is the difference between String initializations by new String() and new String("") in Java?

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

  1. String a = new String();
  2. String b = new String("");

Upvotes: 22

Views: 1496

Answers (7)

Aleksandr Podkutin
Aleksandr Podkutin

Reputation: 2570

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.

Upvotes: 2

TheLostMind
TheLostMind

Reputation: 36304

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.

Upvotes: 20

Boann
Boann

Reputation: 50021

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.

Upvotes: 1

Wires77
Wires77

Reputation: 351

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.

Upvotes: 2

user3248346
user3248346

Reputation:

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.

Upvotes: 1

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

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)

Upvotes: 3

Neeraj Jain
Neeraj Jain

Reputation: 7730

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.

Upvotes: 4

Related Questions