Booleans

Booleans are true or false statements. Unlike strings or numbers, booleans store statements of truth: is what I am saying true or false? For example, if I ask, “Are you a robot?”, this question produces a true or false result, which we call a Boolean. In this case, since you are not a robot (hopefully!), we would produce false. We can also use math operators to create boolean expressions. Here are some examples; however, notice the unusual symbols for “equal to” and “not equal to”:

OperatorDescriptionOperatorDescription
<Less than>Greater than
<=Less than or equal to>=Greater than or equal to
==Equal to!=Not equal to

As usual, use System.out.println to print out your results:

System.out.println(10 < 8);
System.out.println((3 * 6) == (32 - 14));

Working Together

Delete all your System.out.println text in your code. Try guessing the answers to the following expressions. Use System.out.println to check your answers.

  • 54 < (10 + 32)
  • (37 / 5) == 7
  • “Hello” + “World” == “Hello World”
  • false == false

Booleans Operators

You can also connect boolean expressions together using the && (AND) and the || (OR) operator. For example, suppose I ask: “Are you a human, and is Nuvi a robot?” The word “and” connects the two true-false questions together. In this case, since it is true that you are a human, and it is also true that Nuvi is a robot, then the overall result is true. Here’s a chart that describes what happens when we connect booleans together:

ExpressionResultExpressionResult
true && truetruetrue || truetrue
true && falsefalsetrue || falsetrue
false && truefalsefalse || truetrue
false && falsefalsefalse || falsefalse

To summarize, && requires both Boolean expressions to be true, while || only requires one of the two Boolean expressions to be true. Here are some more examples: