What is the difference between null
and the ""
(empty string)?
I have written some simple code:
String a = "";
String b = null;
System.out.println(a == b); // false
System.out.println(a.equals(b)); // false
Both statements return false
. It seems, I am not able to find what is the actual difference between them.
A reference to an empty string
""
points to an object in the heap - so you can call methods on it.But a reference pointing to
null
has no object to point in the heap and thus you'll get aNullPointerException
.