Incremental Java
boolean

boolean types

Remember we said that a type was a set of values. You can give a type to a box. This restricts the possible values of the box to one value from the set.

So far, we've seen the following types: int, double, and char. All of these types have large sets. In particular, double is very, very large (about 1018 different values). char is small (about 65,000 values, though we only use about 128 of them).

We're going to look at a type with only two values. boolean only has two values: true and false.

We use Boolean type as the result of comparisons. We can ask if two numbers are equal, or if one number is greater than another. The result is boolean.

Only two literals!

Yes, only two. true and false.

If you know C or C++, you know they don't use boolean types. Instead, int values are used for boolean as well as integers. In situations where you need a Boolean value, C treats 0 as false, and any other int as true.

Personally, I like the boolean type, so that you don't have two meanings for int values. When you want to use true and false, you use boolean. Fortunately, the Java people agree.