1

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.

12
  • 3
    I know that it could be explained with something like "Boolean is an object" - well that's the idea yes. Boolean actually even extends Object (like all objects) and by design the default value of an Object is null... Making an exception would be quite couterintuitive me thinks.
    – assylias
    Dec 13, 2012 at 18:02
  • 6
    null and false are not the same concept and one can't be wrapped into the other. Reference variables need to be able to be null, and combining null with false would be a dangerous thing to do. But please feel free to create your own language which fixes this, and then show how your language is better than Java. Dec 13, 2012 at 18:02
  • 1
    null means that you don't have a value, it's not the same thing as false Dec 13, 2012 at 18:03
  • There are uses for both two-state logic (boolean) and three-state logic (Boolean). The third state is often used to mean "not known", "not available" or "not applicable".
    – NPE
    Dec 13, 2012 at 18:06
  • 1
    @assylias But that's if you have an Integer object. What if you have no object at all?
    – durron597
    Dec 13, 2012 at 18:09

7 Answers 7

11

null represents the absence of a value, whereas true and false are 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 Boolean can be null is because it's an object reference, not a boolean value itself, a confusion that autoboxing might be causing for you. It used to be that you had to manually create Boolean objects to contain boolean values and manually extract the boolean values from Boolean objects. Since Java 5, the compiler will take care of this for you.

The value of the boolean contained by a Boolean object can either be true or false, but an object variable may not be pointing to an object at all, in which case the value of the reference is null.

Why is there a difference between boolean and Boolean you 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.

4
  • That's my point, it would be nice if Java evaluate a null reference for a Boolean to false, just to maintain the 2-state behavior, imho. BTW: I'll accept your answer because it is the most complete at this time, also, my question maybe is not a real question at all. Thanks.
    – caarlos0
    Dec 13, 2012 at 18:18
  • It's a totally real question. I think it's a good question, and in many languages, the behavior you're talking about is exactly what happens. Java is rather pedantic when it comes to types. It took many years before they even included autoboxing, probably because it muddles the distinction between primitives and objects in a way that's not quite natural, which saves time and boilerplate, but can seem inelegant, as you've noticed.
    – acjay
    Dec 13, 2012 at 18:26
  • That's exactly what I'm trying to say... but looks like some folks don't like it... thanks again.
    – caarlos0
    Dec 13, 2012 at 18:33
  • Using Boolean for null represents the absence of a precise requirement definition. I haven't seen a reasonable usage of Boolean yet.
    – Herr Derb
    Jun 18, 2020 at 13:23
3

If the tri-valued nature of Boolean isn't right for you, don't use it. Use boolean. There are times when you need Boolean, when you are working with an Object-oriented interface. When that happens, you use Boolean.

  • boolean is a primitive type which represents the concept you are describing, a type with two possible values: true and false.

  • Boolean is different type. It's an Object which holds a boolean value. If the object doesn't exist, then the reference is null. It's not the boolean object which is null, it's the reference to a boolean object which is null.

Though their names are similar, don't conflate boolean and Boolean. They are distinct types for good reason.

1
  • I think that's not that simple... if I need a Map<String, boolean>, like this gist.github.com/4278518 , I can't don't use Boolean.
    – caarlos0
    Dec 13, 2012 at 18:31
2

Since you have a boolean which can be two states, it doesn't make sense to use Boolean unless

  • you need the three states
  • you are using an API which means you have to.

In general, always use a primitive if you can unless a) you need a null value b) you have to due to an API.

1
  • 1
    or c: Need to use a boolean type with generics.
    – caarlos0
    Dec 13, 2012 at 18:26
1

What you imply is that a wrapper should mimic the strict behavior of the primitive 'real' boolean. Thats the problem. A Boolean is not conceptually the same as a boolean (note capitalization).

The primitive boolean provides what is described in Wikipedia as a boolean.

The Boolean (java.lang.Boolean) is not called a wrapper just for show. It is conceptually an Object that contains a boolean. So references to a Boolean are references, nothing in common with a boolean. Thus the rules for references apply, not primitives.

Its quite simple and logical if you take this viewpoint.

0

You are confusing with primitive boolean and primitive wrapper Boolean. Indeed boolean can be true or false only.

Boolean instance can hold true or false too. But if instance does not exist the reference is null as any not initialized reference.

0

null means "I don't have an object here". The Boolean true object is a wrapper around the primitive boolean true, but it is still an object. Note that the following code throws a NullPointerException

public static void main(String[] args) {
  Boolean myBool = null;
  boolean primBool = myBool;

  System.out.println(primBool);
}
0

Null makes it nullable , when you create a database for example it has a table with below columns :

Col-1 : Hour (nvarchar)

Col-2 : Minute (nvarchar)

Col-3 : IsDay (bool)

So if the hour is 18 and minute is 00 what is the value for IsDay ? (it is nor true or false) it is null at this time...

Not the answer you're looking for? Browse other questions tagged or ask your own question.