What is the difference between null
and the \"\"
(empty string)?
I have written some simple code:
String a = \"\";
String b
In Java a reference type assigned null
has no value at all. A string assigned ""
has a value: an empty string, which is to say a string with no characters in it. When a variable is assigned null
it means there is no underlying object of any kind, string or otherwise.
When you write
String a = "";
It means there is a variable 'a' of type string which points to a object reference in string pool which has a value "". As variable a
is holding a valid string object reference, all the methods of string can be applied here.
Whereas when you write
String b = null;
It means that there is a variable b
of type string which points to an unknown reference. And any operation on unknown reference will result in an NullPointerException
.
Now, let us evaluate the below expressions.
System.out.println(a == b); // false. because a and b both points to different object reference
System.out.println(a.equals(b)); // false, because the values at object reference pointed by a and b do not match.
System.out.println(b.equals(a)); // NullPointerException, because b is pointing to unknown reference and no operation is allowed
"" and null both are different . the first one means as part of string variable declaration the string constant has been created in the string pool and some memory has been assigned for the same.
But when we are declaring it with null then it has just been instantiated jvm , but no memory has been allocated for it. therefore if you are trying to access this object by checking it with "" - blank variable , it can't prevent nullpointerexception . Please find below one use-case.
public class StringCheck {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "siddhartha";
String s2 = "";
String s3 = null;
System.out.println("length s1 ="+s1.length());
System.out.println("length s2 ="+s2.length());
//this piece of code will still throw nullpointerexception .
if(s3 != ""){
System.out.println("length s3 ="+s3.length());
}
}
}
String s = "";
s.length();
String s = null;
s.length();
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 a NullPointerException
.
This concept can be better understood from mathematics. Have you ever tried dividing a number (not zero) by 0 using a calculator e.g 7/0? You will get a result that looks like something this: undefined
, not a number
, null
etc. This means that the operation is impossible, for some reasons (let's leave those reasons to be discussed another day).
Now, perform this: 0/7. You will get the output, 0. This means that the operation is possible and can be executed, but you the answer is just 0 because nothing is left after the division. There is a valid output and that output is zero.
In the first example, not only was the output invalid, the operation was not possible to execute. This is akin to null
string in java. The second example is akin to empty
string.
here a is an Object
but b(null)
is not an Object it is a null reference
System.out.println(a instanceof Object); // true
System.out.println(b instanceof Object); // false
here is my similar answer