Java for students logo
Explore the English language on a new scale using AI-powered English language navigator.

True/false

There are many situations when one deals with true/false questions in the program. Is 2 greater than 3? Does 2 + 2 equal to 4? Does guess equal to secretNumber? (Remember number guessing game.) In this lesson we'll show how to store answers in boolean variables and construct more complicated conditions.

Boolean type

Recall primitive types table. One of them is the boolean type, variables of which could only be set to true or false. Let's illustrate it on example:

public class TrueFalse {

      public static void main(String[] args) {

            boolean boolVar;

            boolVar = true;

            System.out.println("boolVar = " + boolVar);

            boolVar = false;

            System.out.println("boolVar = " + boolVar);

      }

}

boolVar = true

boolVar = false

There are more interesting things we can do. One can assign a result of comparison to boolean variable:

public class TrueFalse {

      public static void main(String[] args) {

            boolean boolVar;       

            boolVar = (2 < 3);

            System.out.println("2 < 3? Answer is " + boolVar);

            boolVar = (4 == 5);

            System.out.println("4 == 5? Answer is " + boolVar);

      }

}

2 < 3? Answer is true

4 == 5? Answer is false

Logical connectives

What if one wants to ask: "Is (2 smaller than 3) and (3 smaller than 4)?". Java provides several tools to build a combination of conditions. In general, one needs three of them:

Logical connective
Notation in Java
Example
Explanation
Negation (NOT) ! !(2 == 3) if operand is false
Conjunction (AND) && (2 < 3) && (3 < 4) if both of operands are true
Disjuction (OR) || (2 < 3) || (5 > 10) if at least one of operands is true

Truth table for AND and OR is:

a
b
a && b (AND)
a || b (OR)
false false false false
false true false true
true false false true
true true true true

Let's see logical connectives in action:

public class TrueFalse {

      public static void main(String[] args) {

            boolean boolVar;

            boolVar = (2 < 3) && (3 < 4);

            System.out.println("(2 < 3) && (3 < 4): " + boolVar);

            boolVar = (4 == 5) || (2 + 2 == 4);

            System.out.println("(4 == 5) || (2 + 2 == 4): " + boolVar);

            boolVar = (2 + 2 == 3) && (2 < 3 || 5 > 7);

            System.out.println("(2 + 2 == 3) && (2 < 3 || 5 > 7): " + boolVar);

      }

}

(2 < 3) && (3 < 4): true

(4 == 5) || (2 + 2 == 4): true

(2 + 2 == 3) && (2 < 3 || 5 > 7): false

And a bit more complicated program printing truth table for AND:

public class TruthTable {

      public static void main(String[] args) {

            boolean a, b;

            a = false;

            System.out.println("a   b     a && b");

            do {

                  b = false;

                  do {

                        System.out.println(a + "\t" + b + "\t" + (a && b));

                        b = !b;

                  } while (b);

                  a = !a;

            } while (a);

      }

}

a       b       a && b

false   false   false

false   true    false
true    false   false
true    true    true

Note. When using boolean variable as a condition in statement, don't compare it with true:

public class TrueFalse {

      public static void main(String[] args) {

            boolean a = true;

            do {

                  a = !a;

            } while (a == true); // not a mistake, but bad style

      }

}

 

It's already a boolean value, just use it:

 

public class TrueFalse {

      public static void main(String[] args) {

            boolean a = true;

            do {

                  a = !a;

            } while (a); // much better

      }

}

We hope the lesson helped you to master true/false stuff in Java. Use it in your programs.