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" );
       }
    }
}
5
  • 1
    I'm guessing String b = new String() is the same as String b = "". Commented May 18, 2015 at 3:39
  • You can instantiate the String in many other ways as well. Commented May 18, 2015 at 3:41
  • @Ben: Not really. It does create a new instance (which assigning to "" will not) Commented May 18, 2015 at 3:41
  • Interesting, I just saw this too, learning something new everyday~ Commented May 18, 2015 at 3: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. Commented May 18, 2015 at 4:46

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

I like the cup analogy ^
Though according to @Thilo's response on the original question and this thread, String b = "" doesn't necessarily create a new instance of the String object, it may refer to an existing item in the intern pool.
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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.