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.
here
a is an Object
butb(null)
is not an Object it is a null referencehere is my similar answer
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 toempty
string.What your statements are telling you is just that "" isn't the same as null - which is true. "" is an empty string; null means that no value has been assigned.
It might be more enlightening to try:
"" is still a string, meaning you can call its methods and get meaningful information. null is an empty variable - there's literally nothing there.
"" 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.
}
null means the name isn't referencing any instantiated object. "" means an empty string.
Here a is referencing some object which happens to be an empty string. b isn't referencing any object as it's null.
"" is an actual string, albeit an empty one.
null, however, means that the String variable points to nothing.
a==b
returns false because "" and null do not occupy the same space in memory--in other words, their variables don't point to the same objects.a.equals(b)
returns false because "" does not equal null, obviously.The difference is though that since "" is an actual string, you can still invoke methods or functions on it like
a.length()
a.substring(0, 1)
and so on.
If the String equals null, like b, Java would throw a
NullPointerException
if you tried invoking, say:b.length()
If the difference you are wondering about is == versus equals, it's this:
== compares references, like if I went
That would output false because I allocated two different objects, and a and b point to different objects.
However,
a.equals(b)
in this case would return true, becauseequals
for Strings will return true if and only if the argument String is not null and represents the same sequence of characters.Be warned, though, that Java does have a special case for Strings.
You would think that the output would be
false
, since it should allocate two different Strings. Actually, Java will intern literal Strings (ones that are initialized like a and b in our example). So be careful, because that can give some false positives on how == works.