Boolean type

The boolean is a data type that has only two possible values: false and true. This is also known as the logical type.

This type is a common way in programming languages to represent something that has only two opposite states like on or offyes or no, etc.

If you are writing an application that keeps track of opening and closing a door you’ll find it natural to use boolean to store current door state.

boolean open = true;
boolean closed = false;

System.out.println(open);   // true
System.out.println(closed); // false

Important, you cannot assign an integer value to a boolean variable. In Java, 0 is not the same as false.

Logical operators

Variables of the boolean type are often used to build logical expressions using logical operators. Java has four logical operators NOTANDOR and XOR:

  • NOT is a unary operator that reverses the boolean value. It is denoted as !.
boolean f = false; // f is false
boolean t = !f;    // t is true
  • AND is a binary operator that returns true if both operands are true, otherwise, it returns false. It is denoted as &&.
boolean b1 = false && false; // false
boolean b2 = false && true;  // false
boolean b3 = true && false;  // false
boolean b4 = true && true;   // true 
  • OR is a binary operator that returns true if at least one operand is true, otherwise, it returns false. It is denoted as ||.
boolean b1 = false || false; // false
boolean b2 = false || true;  // true
boolean b3 = true || false;  // true
boolean b4 = true || true;   // true
  • XOR (exclusive OR) is a binary operator that returns true if boolean operands have different values, otherwise, it returns false. It is denoted as ^.
boolean b1 = false ^ false; // false
boolean b2 = false ^ true;  // true
boolean b3 = true ^ false;  // true
boolean b4 = true ^ true;   // false

The XOR operator is used less often than others. Just remember that Java has it. If you really need it, you can use it.

The precedence of logical operators

Below are the logical operations sorted in order of decreasing their priorities in expressions: ! (NOT), ^ (XOR), && (AND), || (OR).

So, the following variable is true:

boolean b = true && !false; // true, because !false is evaluated first

To change the order of execution you can use round brackets (...).

An example: trekking

As an example, let’s write a complex boolean expression that determines the possibility of trekking in summer and in other seasons. Let’s suppose that the right conditions for trekking is warm dry weather in summer and autumn.

boolean cold = false;
boolean dry = true;
boolean summer = false; // suppose now is autumn

boolean trekking = dry && (!cold || summer); // true, let's go to trek!

Do not get confused in the expression above, otherwise, you will go trek in bad weather! A programmer should understand not only arithmetic but also logical operations.

Short-circuiting evaluation

An interesting thing is that the && and || operators don’t evaluate the second argument if it isn’t necessary. When the first argument of the && operator evaluates to false, the overall value must be false; and when the first argument of the || operator evaluates to true, the overall value must be true. So:

  • false && ... -> false, since it is not necessary to know what the right-hand side is;
  • true || ... -> true, since it is not necessary to know what the right-hand side is.

Leave a Reply

Your email address will not be published.