tags:

views:

397

answers:

7

Hi, In Java are these 2 statements the same?

String a = null;
String b = "";

It feels a bit of a dumb question but have a complete mental block currently.
Dean

+26  A: 

The empty string and null are different. The empty string is a string with no characters, null is not having a string at all.

You can call methods on an empty string but if you try to call a method on null you will get an exception.

public static void main(String[] args)
{
    String a = null;
    String b = "";
    System.out.println(b.length());
    System.out.println(a.length());
}

Output:

0
Exception in thread "main" java.lang.NullPointerException
        at Program.main(Program.java:12)
Mark Byers
+2  A: 

No, an empty string is not null.

Paul Tomblin
+2  A: 

They are most definitely not the same. Your String variable acts as a reference to an object in memory, and if it's set to null, it's not pointing to anything. If it's set to the empty-string value, it's pointing to that.

In my own coding, I generally set a String to "" instead of to null unless I have a special need for null. There are some libraries like Apache Commons that include helper classes like StringUtils that will collapse a check for null, the empty string, and even just whitespace into one call: StringUtils.isBlank(), StringUtils.isNotBlank(), etc. Pretty handy. Or you can write your own helper methods to do similar pretty easily.

Good luck as you progress in Java!

NeantHumain
Or just call String's own isEmpty(), no need to mess with StringUtils isBlank().
Steve Kuo
That method has only been available since Java 6 and, unlike the StringUtils methods, is not null safe.
NeantHumain
A: 

this is not as dumb as it sounds. it bothers even seasoned programmers. in many real world projects people often write something like if(s==null || s.isEmpty()), i.e. people treat null and "" as semantically equivalent .

irreputable
atamanroman
As strings are immutable in c# it seems odd to me that all strings of the same value aren't the same string (thus making == okay for strings). However, it is important that ==null *isn't* true because otherwise how do we distinguish between non-provided values and provided-but-empty values.
Graphain
+1  A: 

null means it refers to nothing, while empty string is a special string with zero length.

fastcodejava
+1  A: 

The third possibility is:

String c;

All three are different, of course.

Loadmaster
a and c aren't are they?
Graphain
@Graphain: If they are instance members they are the same, but if they are statements in a method body then they are not the same. In the latter case attempting to read the variable c before it is initialized will give a compile error.
Mark Byers
My comment went missing somehow. They are the same, it's just c will give an error (depending on your compile settings i believe) because you haven't explicitly told the compiler "i want null here" so it thinks you've forgotten. However, they are exactly equivalent, i.e. c==a.
Graphain
Nope, an uninitialized local variable is not assigned any value by default. The compiler will warn you if you try to use its value before it's been assigned one.
Loadmaster
A: 

String a = null;
String b = "";

The first statement in java initialises a variable handle. there is no memory allocated for data to be saved.

The second statement shows two objects the first object being the handle (b) and second object being "" (if we ignore the higher concepts of string pooling in java where string is mutable and jvm gives out pooled instances of string)

thus the two lines are not same.

frictionlesspulley