itoilet
itoilet

Reputation: 71

Java: Beginner question regarding Strings

When creating a String in Java, what is the difference between these two:

String test = new String();
test = "foo";

and

String test = "foo";

When do I need to use the keyword new? Or are these two basically the same and they both create a new String object?

Upvotes: 7

Views: 105

Answers (3)

Yogesh_D
Yogesh_D

Reputation: 18764

Here's a sample program to help you understand how strings work in Java.

import java.util.Objects;

public class TestStrings {

    public static void main(String[] args) {
        String test = new String();
        System.out.println("For var test value is '"+ test+ "' and object identity is "+ System.identityHashCode(test));
        test = "foo";
        System.out.println("For var test after reassignment value is '"+ test+ "' and object identity is "+ System.identityHashCode(test));
        String test2 = "foo";
        System.out.println("For var test2 value is '"+ test2+ "' and object identity is "+ System.identityHashCode(test2));
        String test3 = new String("foo");

        System.out.println("For var test3 value is '"+ test3+ "' and object identity is "+ System.identityHashCode(test3));
    }
}

Run this to see what happens for the identity Hash code printed for variables test, test2 and test3.

Basically Java tries to optimize how strings are created when they are created as literals. Java tries to maintain a pool of strings and if you use the same literal again it uses the same object from this pool of strings. This can be done because Strings in java are immutable.

You can read further on this at this question What is Java String interning?

Upvotes: 0

Marvin Klar
Marvin Klar

Reputation: 1917

new String() will create a new instance of the object string with an own identity hash code. When creating a string like this String string = "myString"; Java will try to reuse the string by searching the already created string, for that exact string. If found, it will return the same identity hash code of this string. This will cause, that if you create e.g. a identity hash code of the string, you will get the same value.

Example:

public class Stringtest {
   public static void main(String[] args) {
      final String s = "myString";
      final String s2 = "myString";
      final String otherS = new String("myString");

      //S and s2 have the same values
      System.out.println("s: " + System.identityHashCode(s));
      System.out.println("s2: " + System.identityHashCode(s2));

      //The varaible otherS gets a new identity hash code
      System.out.println("otherS: " + System.identityHashCode(otherS));
   }
}

In most cases you don't need to create a new object of a string, cause you don't have static values when working with e.g. HashMaps or similar things.

So, only create new strings with new String when really needed. Mostly use String yourString = "...";.

Upvotes: 0

Mureinik
Mureinik

Reputation: 311143

In the first snippet, you create a new empty string, and then immediately overwrite it with a string literal. The new string you created is lost, and will eventually be garbage-collected.
Creating it is pointless, and you should just use the second snippet.

Upvotes: 4

Related Questions