Java is one of the languages that explicitly supports a primitive Boolean variable type.

Boolean variables

edit

A primitive variable can be declared to be of type boolean and can have assigned to it one of the two boolean literals true or false.

    boolean a = true;
    System.out.println("a is " + a);

The above code would print a is true.

Boolean Operators

edit

Relational operators

edit

Java supports the full set of relational operators, as described in the general introduction to Boolean variables. Relational operators can be used to compare any two type-compatible primitives.

At the moment, we have only covered two categories of primitives, numeric and boolean.

Any two values that are numeric (of any type) are, or will be made, compatible. The full suite of relational operators may be used without restriction.

As true and false are not numeric values, neither can be said to be greater or less than the other, in the strict mathematical sense. Only the is equal (==) and is not equal (!=) operators may be used for boolean values. The code below will print c = false.

        boolean a = true;
        boolean b = false;
        
        boolean c = a == b;
        System.out.println("c = " + c);

Note: the use of a double equal sign for is equal and a single equal sign for assignment is a common source of programming errors. It is very easy to accidentally type just one equal sign character in situations where you want two.

Conditional operators

edit

Conditional operators operate only on boolean values. Java has three, conditional-and, conditional-or, and conditional-not (there is another kind of and, or and not that will be covered later, thus the distinction).

The operator for conditional-and is a pair of adjacent ampersand characters &&. The result of conditional-and is false unless both operands are true. The conditional-and operator has one unusual property that can be both useful and confusing: because the result will always be false if the value of either operand is false, the left operand will always be evaluated but the right hand operator will only be evaluated if the left hand operator evaluates to the value true.

The operator for conditional-or is a pair of pipe characters ||. The result of conditional-or is true unless both operands evaluate to false. If the value of the left-hand operand evaluates to the value true then the right-hand operand is not evaluated.

The operator for conditional-not is a single exclamation point !. This is a unary operation and the result is the opposite of the value of the operand.

Boolean expressions

edit

Not everything is quite as simple as is x greater than two.

Take, for example, leap years. How would you write a Boolean expression that evaluates to true if the value of an integer variable year is that of a leap year in the Gregorian calendar? (n.b. there are several "right" ways to do it)

One way to start it is the obvious way: is year divisible by 4? You would use Java's integer remainder operator (%) and compare the result to 0: (year % 4) == 0. The parentheses are not strictly necessary, but improve readability.

Next, we could check to see that year is not divisible by 100: (year % 100) != 0. Finally, check for year is divisible by 400: (year % 400) == 0. Now combine them all together. The first two checks must both be true for it to be a leap year but if the third check is true then the result will always be true. This fact suggests that perhaps we could put the third check first (to take advantage of short circuiting).

((year % 400) == 0) or ((year % 4) == 0 and (year % 100) != 0) 

Now we just need to substitute the Java operators for "or" and "and":

((year % 400) == 0) || ((year % 4) == 0 && (year % 100) != 0) 

Exercise Try the expression out in a program. Declare a variable year and assign it a value, then have the program print out a message telling whether or not the value is a leap year.

If statements

edit

The most common use of boolean values will appear in if statements. These statements allow you to designate units of code that will only execute if the boolean condition is true. For example, you can print a message if a given year is a leap year, and remain silent otherwise:

  if ( ((year % 400) == 0) || ((year % 4) == 0 && (year % 100) != 0)  )
  {
    System.out.println("This is a leap year.");
  }

If statements and other flow control constructs will be discussed in more detail in the flow control module.


Project: Introduction to Programming in Java
Previous: Integer variables — Java Programming/Boolean variables — Next: Java Classes