So, according to wikipedia and others, a boolean values should have only 2 states: 0 or 1; true or false; yes or no; and so on…
OK, correct me if I’m wrong, but, Java Boolean wrapper let developers have 3 states: true, false and null.
Isn’t that wrong?
I know that it could be explained with something like “Boolean is an object” etc etc, but, it would be really great if javac automagically “wrap” null to false, IMHO.
My point is: this behavior let developers do a lot of crap, because they use Boolean instead of a proper object type.
Anyway, what do you think about it? Am I following a wrong line of thinking.. or is it really “wrong”?
EDIT
I know what is null, the differences between boolean and Boolean, etc etc etc (I have 3-year+ exp with java).
My point is about the concept itself, like in Ruby, for example, if I do something like if something, if this something instance is nil, it will be false.
And I’m not saying that Java is crap or something like, I use it everyday.
nullrepresents the absence of a value, whereastrueandfalseare definite values. It’s the difference between knowing something, either affirmatively or negatively, and not knowing it at all. That’s one way to think of it.Technically the reason a
Booleancan benullis because it’s an object reference, not abooleanvalue itself, a confusion that autoboxing might be causing for you. It used to be that you had to manually createBooleanobjects to containbooleanvalues and manually extract thebooleanvalues fromBooleanobjects. Since Java 5, the compiler will take care of this for you.The value of the
booleancontained by aBooleanobject can either betrueorfalse, but an object variable may not be pointing to an object at all, in which case the value of the reference isnull.Why is there a difference between
booleanandBooleanyou might ask? Well, in Java, unlike a lot of newer languages, the primitive types are not classes, and primitives are not objects. When you want to treat them as such, like when you want to pass by reference or call methods on them or put them in collections, you have to box them up in their respective primitive wrapper classes.