Skip to main content

Section 2.7 Boolean Expressions

Boolean variables or expressions can only have true or false values.

Subsection 2.7.1 Testing Equality (==)

The operators == and != (not equal) can be used to compare values. They return true or false boolean values.

Note 2.7.1.

One = sign changes the value of a variable. Two == equal signs are used to test if a variable holds a certain value, without changing its value!
The following code shows how == is used with primitive types like int.

Checkpoint 2.7.2.

What will the code below print out? Try to guess before you run it! Note that 1 equal sign (=) is used for assigning a value and 2 equal signs (==) for testing values.

Subsection 2.7.2 Relational Operators (<, >)

The Relational Operators below in Java are used to compare numeric values or arithmetic expressions. Although some programming languages allow using relational operators like < to compare strings, Java only uses these operators for numbers, and uses the string methods compareTo() and equals() for comparing String values.
If you have trouble telling < and > apart, think of a number line and think of < and > as arrows; < (less than) points towards 0 and smaller numbers on the number line and > (greater than) points towards the larger numbers on the number line. Or remember that < starts with the smaller (less) point and > starts with the open wide (greater) side. With <= and >=, remember to write the two symbols in the order that you would say them “less than” followed by “or equal to”.
Coding Exercise

Checkpoint 2.7.3.

Try to guess what the code below will print out before you run it.
Check your understanding

Checkpoint 2.7.4.

Subsection 2.7.3 Testing with mod (%)

Here are some boolean expressions that are very useful in coding and mod is used in many of them:
// Test if a number is positive
(number > 0)
//Test if a number is negative
(number < 0)
//Test if a number is even by seeing if the remainder is 0 when divided by 2
(number % 2 == 0)
//Test if a number is odd by seeing if there is a remainder when divided by 2
(number % 2 > 0)
//Test if a number is a multiple of x (or divisible by x with no remainder)
(number % x == 0)

Checkpoint 2.7.5.

Try the expressions containing the % operator below to see how they can be used to check for even or odd numbers. All even numbers are divisible (with no remainder) by 2.
The modulo operator is a powerful tool in programming. Because of its versatility, it’s important to become familiar with how it works and how it can be applied in different problem-solving scenarios
  • Use it to check for odd or even numbers (num % 2 == 1) is odd and (num % 2 == 0) is even. Actually, you can use it to check if any number is evenly divisible by another (num1 % num2 == 0)
  • Use it to get the last digit from an integer number (num % 10 = last digit on right).
  • Use it to get the number of minutes left when you convert to hours (num % 60).
  • Use it whenever you have limit in the value, and you need to wrap around to the front if the value goes over the limit (num % limit).

Subsection 2.7.4 Summary

  • Primitive values and reference values can be compared using relational operators (i.e., == and !=) in Java.
  • Arithmetic expression values can be compared using relational operators (i.e., <, >, <=, >=) in Java.
  • An expression involving relational operators evaluates to a Boolean value of true or false.
You have attempted of activities on this page.