Why does autoboxing in Java allow me to have 3 possible values for a boolean?

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-05-27T17:11:13Z Indexed on 2010/05/27 17:11 UTC
Read the original article Hit count: 320

Filed under:
|
|

Reference: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

If your program tries to autounbox null, it will throw a NullPointerException.

javac will give you a compile-time error if you try to assign null to a boolean. makes sense. assigning null to a Boolean is a-ok though. also makes sense, i guess.

but let's think about the fact that you'll get a NPE when trying to autounbox null. what this means is that you can't safely perform boolean operations on Booleans without null-checking or exception handling. same goes for doing math operations on an Integer.

for a long time, i was a fan of autoboxing in java1.5+ because I thought it got java closer to be truly object-oriented. but, after running into this problem last night, i gotta say that i think this sucks. the compiler giving me an error when I'm trying to do stuff with an uninitialized primitive is a good thing.

I think I may be misunderstanding the point of autoboxing, but at the same time I will never accept that a boolean should be able to have 3 values. can anyone explain this? what am i not getting?

© Stack Overflow or respective owner

Related posts about java

Related posts about autoboxing