Difference between null and empty string

211,321

Solution 1

String s1 = ""; means that the empty String is assigned to s1. In this case, s1.length() is the same as "".length(), which will yield 0 as expected.

String s2 = null; means that (null) or "no value at all" is assigned to s2. So this one, s2.length() is the same as null.length(), which will yield a NullPointerException as you can't call methods on null variables (pointers, sort of) in Java.

Also, a point, the statement

String s1;

Actually has the same effect as:

String s1 = null;

Whereas

String s1 = "";

Is, as said, a different thing.

Solution 2

Null means nothing. Its just a literal. Null is the value of reference variable. But empty string is blank.It gives the length=0. Empty string is a blank value,means the string does not have any thing.

Solution 3

No method can be invoked on a object which is assigned a NULL value. It will give a nullPointerException. Hence, s2.length() is giving an exception.

Solution 4

When Object variables are initially used in a language like Java, they have absolutely no value at all - not zero, but literally no value - that is null

For instance: String s;

If you were to use s, it would actually have a value of null, because it holds absolute nothing.

An empty string, however, is a value - it is a string of no characters.

String s; //Inits to null
String a =""; //A blank string

Null is essentially 'nothing' - it's the default 'value' (to use the term loosely) that Java assigns to any Object variable that was not initialized.

Null isn't really a value - and as such, doesn't have properties. So, calling anything that is meant to return a value - such as .length(), will invariably return an error, because 'nothing' cannot have properties.

To go into more depth, by creating s1 = ""; you are initializing an object, which can have properties, and takes up relevant space in memory. By using s2; you are designating that variable name to be a String, but are not actually assigning any value at that point.

Share:
211,321
Shakeeb Ayaz
Author by

Shakeeb Ayaz

Updated on August 14, 2020

Comments

  • Shakeeb Ayaz
    Shakeeb Ayaz over 3 years

    What is the difference between a null string (String s = null) and an empty string (String s = "")?

    This is what I have:

    String s1 = ""; //print statement does not print any thing for s1 but s1.length()=0
    String s2 = null;//print statement prints "null" for s2  but s2.length() gives exception
    

    What does it mean?

  • carlosdc
    carlosdc almost 11 years
    this explanation is really broken. a value type cannot be null.
  • Singular1ty
    Singular1ty almost 11 years
    @carlosdc Can you elaborate? What do you mean a value type cannot be null?
  • carlosdc
    carlosdc almost 11 years
    try int i = null; in java.
  • Singular1ty
    Singular1ty almost 11 years
    Oh you mean that you can't assign a value of null?
  • carlosdc
    carlosdc almost 11 years
    exactly. not to int, char, double, float, short or boolean. you're trying to simplify the explanation too much, and it is just wrong.
  • Ron Dahlgren
    Ron Dahlgren almost 11 years
  • Singular1ty
    Singular1ty almost 11 years
    @carlosdc Fixed up the my post a bit.
  • Onic Team
    Onic Team over 4 years
    @Alexis Wilke : when String s = null+"a"; it gives output nulla but null.concat("a") it gives the null pointer exception. what is the reason in my first case null+""; is working
  • Alexis Wilke
    Alexis Wilke over 4 years
    More or less, when you do <object> + <string> the compiler attempts a conversion of <object> to a string. Kind of like <object>.toString() + <string>, although for null that's a special case since you can't call a function from null.