autobox « Boolean « Java Data Type Q&A

1. java: boolean instanceOf Boolean?

I'm a bit confused: I have a function, that takes an Object as argument. But the compiler does not complain if I just pass a primitive and even recognizes a boolean ...

2. Booleans, conditional operators and autoboxing

Why does this throw NPE

public static void main(String[] args) throws Exception {
    Boolean b = true ? returnsNull() : false; // NPE on this line.

3. Boolean != false

In Java, you would usually say that if(someBool != false) is the same as if(someBool) But what if someBool is not of type boolean but Boolean, and its value is null?

4. Autoboxing and Booleans

5. autoboxing booleans in comparisons

Looking at the rules it seems to me the javac compiler behave inconsistently, or even more, IMHO, buggy. The jls rules state about unboxing only when one of the operands is of type Boolean. None of the operands is of type Boolean in my code. So none of the if statements should compile. B