Reputation: 2291
I have the following code snippet:
Boolean var = false;
boolean var1 = (var = null);
if (var1) {
// It compiles
}
if (var = null) {
// It compiles
}
Why does it compile?
In the Boolean
class I found the following:
public static boolean parseBoolean(String s) {
return ((s != null) && s.equalsIgnoreCase("true"));
}
Does it mean that is null
considered as false
? Why the result of =
operation on boolean
is false
? What the practical reason of this behavior?
Upvotes: 2
Views: 21690
Reputation: 18420
Assigning null
to a native type (like boolean
, int
, etc.) will result in a NullPointerException.
Upvotes: 9
Reputation: 312
First of all your code will not compile. It will give you a compilation error on the if (null = var) {
line.
Second thing is that while initializing a primitive data type, you can't assign a null
value. If you assigning it on compile time, it will give you a compilation error. But if you doing it on run time, it gives a NullPointerException. Just like that boolean b = null; is not valid in Java. But Boolean bObj = null; is valid. While in your code when your are doing (var = null) it actually returns null
. Hence, boolean var1 = (var = null)
becomes boolean var1 = null
. Here, the variable var1
is assigned with null
value with a run time basis. So, it will give a NullPointerException.
Upvotes: 1
Reputation: 863
Code would be written like this:
Boolean var = false;
boolean var1 = (var == null);
if (var1) {
// It compiles
}
if (var == null) {
// It compiles
}
The reason of var != null
is that null
and Boolean.FALSE
do not share the same memory.
Upvotes: 0
Reputation: 57124
Regarding the first question Why does it compile?
It will give a compile error since if (null = var)
is not valid java code. You cannot assign something to null
, you can only ever assign null
to something.
You might want to use the ==
instead comparing var
and null
for equality.
Beyond that, at runtime you will get an NPE
as @Ctx already correctly mentioned. The line boolean var1 = (var = null);
will firstly assign null
to var
, then the assignment operator =
will return what it has just assigned (null
) and try to assign that to the boolean var1
, which throws a NullPointerException.
Does it mean that is
null
considered asfalse
?
Not really. Only when parsing a String
which is null
this is treated as false
. That is basically the only place / situation.
null = var
to var = null
:Now your code will actually compile and crash with a NullPointerException. Let's go through what happens here step by step:
Boolean var = false;
— A Boolean
object is created by autoboxing the boolean
value false
.
boolean var1 = (var = null);
— The first operation is (var = null)
which assigns null
to var
. =
therefore returns null
. The statement then is "equivalent" to boolean var1 = null
which the compiler would reject.
Unfortunately, the compiler is not capable of deducing that the statement boolean var1 = (var = null);
would always lead to the invalid assignment boolean var1 = null
. The code therefore compiles fine, but crashes at runtime.
Upvotes: 8
Reputation: 155
At first, do you know the difference between Boolean
and boolean
? Because assign null
to boolean
will cause java.lang.NullPointerException
.
Upvotes: 0