Why does boolean data type accept true or false values only, why cant it accept 0 for nothing (which is neither true nor false)?


Because then it'd be a tri-state value, not a boolean.

If you're working in C# and trying to map bool to a nullable database column (which seems to be the usual cause of asking this question), then what you're after is Nullable<bool>, also represented as bool? which allows you to state that the value of the variable is true, false or null. For example:

bool? trueFalseOrUndefined = null;
bool? trueBool = true;
bool? falseBool = false;

To add to my answer, an example of a situation where this might be applicable would be where you have a database containing a table of Product's, each of which is a member of a discete Category (for example: Toys, Books, Chairs), and has a stored property IsCheapestInCategory which is calculated by a batch process once every hour. If a new record was inserted 2 minutes after the batch process was executed, it would be correct for IsCheapestInCategory to be NULL rather than true or false. Following on from that it would be sensible to represent this in your C# code as:

class Product
  public string Name  {get; set;}
  public string Category {get; set;}
  public bool? IsCheapestInCategory {get; set;}

However, it would be incorrect to use a bool? to represent something like 'Yes', 'No', 'Sometimes', as in this case an enumeration would make far more sense with the added advantage of being more understandable to anyone maintaining the code in the future.

11 accepted

Because that is the nature of boolean, it is either true or false, like a bit can be only one or zero. What you are looking for is probably this: Ternary_logic


Boolean represents a logical value. These can only be represented as true or false.

What you are probably looking for is Boost's tribool in C++.

In C#, you can make any struct (built in types save for string are all structs) be able to take a null value by appending the nullable type declaration ?, ie. bool?.

bool? foo = null;

I'm sure there's a similar structure in Java, but I don't know Java, sorry.


They called it Boolean because they only wanted it to store either true or false. So there's actually no scope for anything else.

All discussions are heading towards proving why a Boolean type can only store true or false which is a wrong direction IMHO.

Imagine yourself in a shopping-mall where you're buying yourself a t-shirt and your size is XL. You came back home with a cool t-shirt of size XL. Now you're asking why it is an exact fit to only those with size XL. The answer is simple, because when you bought the t-shirt,you knew your size XL and you wanted to buy the t-shirt of your size that is XL!


It only accepts two values because a proposition can only be true or false.

If you want a third, indeterminate state, then you need ternary logic.


Because that's the definition of the boolean type?


In Java, if you need something that can be true, false or "empty", use the Boolean wrapper class:

Boolean flag = null; // use null to indicate "empty"

flag = true; // will use autoboxing

if (flag == null) {

A boolean represents a single bit, which is either 0 (false) or 1 (true).

A NULL / zero value would only be possible for an object type, which boolean isn't (as it would basically defeat the purpose of the type in the context of boolean algebra).

If I remember correctly, Java does have a Boolean object type, which could conceivably be NULL. (Edit: And Rob's answer shows that C# also has such a beast, bool?.)

However, as I said, from the standpoint of boolean algebra a NULL value does not make any sense.


Booleans are either true or false because computer logic is binary, not fuzzy.
As for the undefined value, there are objects wrapping booleans (ex java.lang.Boolean in java) which can be null.


I agree with most of the responses that a boolean should be true or false only. However, I have had scenarios where a database boolean column type allowed null values and null had meaning to the application (not how I would have designed or implemented it but I still had to handle it).

From the Java point of view:

boolean b; // primitive type - can be true or false
Boolean b2 // reference variable - can be true, false or null

Although b2 could cause NullPointers e.g.

if(b2) {} // can cause a NullPointerException if b2 is null

You could also create a pseudo-boolean datatype using enums e.g.

class Processor {

    enum TernaryBoolean {

    void checkSomething(TernaryBoolean flag) {

        if(flag.equals(TernaryBoolean.UNKNOWN)) {
            // stuff
        } else if(flag.equals(TernaryBoolean.TRUE)) {
            // other stuff
        } else {
            // other stuff again

I used the enum solution as it meant I didn't have to check for nulls everytime I was writing an if statement (although, in reality, all I was really doing was replacing the null check with the UNKNOWN check).


Ahh, what does it all mean? Perhaps a boolean should have real and imaginary parts - after all the answers to many questions can be complex (groan!).


For that you want to use a nullable bool and set it to null to specifiy an undeterminate state.


hey subanki, I wonder if you're also curious about why:

  • can't an int be nothing, why does it have to be "something", like 0, 2, or -2.

  • can't a datetime be null, why must it be a "default" value.

This is because, in languages like c# and Java, you have the notion of reference types, and value types.

bool is a value type, thus it must contain a value.

You could then get into philosophical debates as to why it must be a value type... and I imagine it's to keep the language, be it c# or Java, clean and consistance, to keep it logical, and strict, so it makes "sense".

Of course, as others have mentioned, c# at least has got a round that with the idea of a "nullable" type, which any type can be, and other languages have their ways.

There's an interesting article on types here:


and here:



A valid Boolean may only have two values by definition or it world be no longer Boolean! A Boolean value is a mathematical entity, not just another integer type.

However, of the languages you tagged both C# and Java allow a bool object to have a null value indicating unassigned. You cannot perform operations on such an object without causing an exception. In C++ if you really must you could create a Bool class with sufficient operator overloads to make it do the same (at some probable great cost to efficiency, and limited actual utility). A partial example:

class Bool
       Bool() : null(true) {}
       Bool( bool b ) : state(b), null(false) {}

       // overloads for =, ==, !=, >, <, >=, <= etc.
       // which raise exception as necessary for null operands

    private :
       bool null ;
       bool state ;
} ;

For a start, in C++ (and C and Objective-C), 0 is false and anything else is true. false is also a bool that happens to be 0 when cast to int, and true is a bool that happens to be 1 when cast to int.

What exactly, do you mean by "neither true nor false"?

  • Maybe implies a probability ("Will I win the lottery?")
  • Sort of implies some sort of "fuzzy logic" ("Am I tall?")
  • Unknown implies something that hasn't been calculated yet ("Is it raining?")
  • Don't care implies a lack of preference ("W
  • Mu implies incorrect assumptions in the question ("Have you stopped beating your wife yet?")

Don't forget the traditional 4-state logic (01ZX) or IEEE 1164 9-state logic...


SQL logic is all tri-state: true, false, or null which is interpreted as "unknown". In SQL truth tables:

X     Y     X AND Y  X OR Y
true  true  true     true
true  false false    true
true  null  null     true
false true  false    true
false false false    false
false null  false    null
null  true  null     true
null  false false    null
null  null  null     null

As others have noted, Java Boolean can be TRUE, FALSE, or null. But you can't perform logical operations on null values, you'll throw null pointer exceptions. You have to test for them by hand.

(I don't know about C#'s "bool?". I admit I didn't know that existed until today.)

If you need to handle tri-state logic like that, or if you have some other pseudo-boolean requirement for "true", "false", "maybe", and "ask Bob" or whatever, you should just create your own object that supports the values you need.