2

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?

3
  • 3
    Why don't you start your program and see what happens? Btw: if(null=var) won't compile... Commented Dec 28, 2015 at 11:18
  • if(null=var) never compile. Commented Dec 28, 2015 at 11:19
  • P.S: Var1 might be true. = will asign a value == will compare. In "boolean var1=(var=null);" the parentesis is just a statement. And in "if(null=var){" it should not compile. You can't assing a value to null Commented Dec 28, 2015 at 11:20

5 Answers 5

9

Assigning null to a native type (like boolean, int, etc.) will result in a NullPointerException.

Sign up to request clarification or add additional context in comments.

Comments

8

Regarding the first question Why does it compile?

Unedited version: It does not 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 as false?

Not really. Only when parsing a String which is null this is treated as false. That is basically the only place / situation.

After the edit changing 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.

3 Comments

Mind that OP silenty invalidated your answer by manipulating the question. But since the edit history still exists, this shouldn't be a problem.
"Only when parsing a String which is null this is treated as false" — Do you mean when using Boolean.valueOf(null)?
@MCEmperor OP was talking about code from Boolean.parseBoolean, not sure what valueOf does exactly.
1
  1. First of all your code will not compile. It will give you a compilation error on the if (null = var) { line.

  2. 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.

Comments

0

At first, do you know the difference between Boolean and boolean? Because assign null to boolean will cause java.lang.NullPointerException.

Comments

0

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.

5 Comments

boolean var1=(var=null); compiles fine, but will cause an exception during runtime.
You're right, can you explain why it can complile, for there is no return value of =
There is a return value: the value of the variable. That is why you can also write int a, b, c; a = b = c = 1;
Yes, now I see why var1 = (var = null) have an exception during runtime. The complier don't know the return value of (var = null), so it can compile, but when running, for var = null will return null, and an error will happen when executing expression boolean var1 = null
Correct. The JVM tries to unbox the value of var, but fails, because it can't unbox null into a meaningful value for a primitive type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.