2

The default value of a Java String is 'null'. However, when I instantiate the string, it seems to be an empty string instead. Can somebody explain this please ?

class Stuff
{
    String a;
    String b = new String();
}

class Demo
{
    public static void main( String[] args )
    {
       Stuff s = new Stuff();
       System.out.println( s.a );
       System.out.println( s.b );

       if( s.b.equals( "" ) )
       {
            System.out.println( "true" );
       }
    }
}
Grateful
  • 9,685
  • 10
  • 45
  • 77
  • 1
    I'm guessing `String b = new String()` is the same as `String b = ""`. – Ben May 18 '15 at 03:39
  • You can instantiate the String in many other ways as well. – Thilo May 18 '15 at 03:41
  • @Ben: Not really. It does create a new instance (which assigning to `""` will not) – Thilo May 18 '15 at 03:41
  • Interesting, I just saw [this](http://stackoverflow.com/questions/15646188/best-practice-curiosity-regarding-string-instantiation) too, learning something new everyday~ – Ben May 18 '15 at 03:44
  • in addition to the answers below, null is a language concept meaning "no reference to an object", so when a String, or any object for that matter, is constructed, it can't chose to be null, even if it wanted to be. Logically it can only be an empty string. An instance of a class might define some other way to say that's its empty or not defined, but it can't say "hey I'm actually null". Not at the object reference level. – slipperyseal May 18 '15 at 04:46

3 Answers3

3

The String() constructor Javaodc says,

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.

Which matches your observed behavior.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

In java a default string or a string set as null has no value. By setting a string with "" or with new String() it will create a new instance of the String class. A string which is initiated with "" or new String() is just a String ready to be used. I guess you can think of it as a cup. If something is null you have no cup, but if something, such as this string, is initiated as new String() or "" you now have an empty cup.

Forseth11
  • 1,418
  • 1
  • 12
  • 21
  • I like the cup analogy ^ – Ben May 18 '15 at 03:45
  • Though according to @Thilo's response on the original question and [this thread](http://stackoverflow.com/questions/15646188/best-practice-curiosity-regarding-string-instantiation), `String b = ""` doesn't necessarily create a new instance of the `String` object, it may refer to an existing item in the intern pool. – Ben May 18 '15 at 03:49
0

Well, the string b is not null anymore because you created a String and assigned it to the variable.

If you use the default constructor of String, you will get an empty String:

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.

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#String()

Community
  • 1
  • 1
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94