Skip to main content
Logo image

Section 2.2 Boolean Expressions

45 minutes

Subsection 2.2.1 Testing Equality (==)

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

Note 2.2.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!
Watch the following video which shows what happens in memory as primitive types like int and reference types like Dog are compared with == in a physical model of Java memory.
The following code shows how == is used with primitive types like int.

Activity 2.2.1.

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. Then, add 3 more lines of code that sets year to 15 and prints out whether age is equal to year and whether age is not equal to year.
We can also use == or != to test if two reference values, like Turtle and String objects, refer to the same object. In the figure below, we are creating two separate Turtle objects called juan and mia. They do not refer to same object or turtle. Then, we create a reference variable called friend that is set to mia. The turtle mia will have two ways (references or aliases) to name her – she’s both mia and friend, and these variables refer to the same object (same Turtle) in memory. If two reference variables refer to the same object like the turtle on the right in the image below, the test with == will return true which you can see in the code below.
Figure 2.2.2. Turtle Reference Equality

Activity 2.2.2.

What will the code below print out? Try to guess before you run it! Then, add another Turtle friend2 and set it to juan. Does friend2 == juan? Does friend2 == friend? Print out the Boolean expressions for these.

Subsection 2.2.2 Relational Operators (<, >)

The relational operators below 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 methods compareTo and equals for comparing String values.
If you have trouble telling < and > apart, think of < and > as arrows where the pointy end should point to the smaller value. If < (less than) points towards a smaller number on the left, then it evaluates to true. On the other hand a > (greater than) expression will be true only if the smaller number is on the right hand side. Or maybe you prefer the β€œhungry alligator” mnemonic beloved by elementary school teachersβ€”think of < and > as the mouths of hungry alligators which always want to eat the bigger number; a < or > expression is only true if the alligator is in fact about to eat the bigger number.
To remember the correct order of the two characters in <= and >=, just write them in the same order you would say them in English: β€œless than or equal to” not β€œequal to or less than”.

Activity 2.2.3.

Try to guess what the code below will print out before you run it. Then, set year to 15 and print the boolean expression for whether age is less than or equal to year.
Boolean variables or expressions have true or false values. Relational operators can be used to compare two variables or compare a variable against a constant value or expression. For example, the following Boolean expressions can be used to see whether a number is positive or negative by seeing if it is greater than 0 or less than 0:
// Test if a number is positive
(number > 0)
//Test if a number is negative
(number < 0)

Activity 2.2.4.

Subsection 2.2.3 Testing with remainder (%)

The remainder operator (%) is very useful in coding. The following Boolean expressions can be used to test whether a number is even or odd by seeing if there is a remainder when it is divided by 2 or if it is divisible by another number:
//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)

Activity 2.2.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. Then, add another expression that tests to see if age1 is divisible by 3.

Note 2.2.3.

A warning: because Java’s % is a remainder operator and not a true mathematical modulo operator, you can’t check if a number is odd with the expression num % 2 == 1.
That expression will be true if num is positive and odd and false when num is even, both of which are correct. But if num is negative and odd, its remainder when divided by 2 is -1, not 1 and this expression will evaluate to false. Thus you should always use num % 2 != 0 to check if num is odd.

Subsection 2.2.4 Coding Challenge : Prime Numbers POGIL

We encourage you to do this activity as a POGIL (Process Oriented Guided Inquiry Learning) group activity or using Think-Pair-Share collaboration. POGIL groups are self-managed teams of 4 students where everyone has a POGIL role and works together to solve the problems, making sure that everyone in the team participates and learns.
In this activity, you will write a Java program that uses boolean expressions to determine if a number is prime. A prime number is an integer number that is only divisible by 1 and itself. For example, 3 is a prime number because it’s only divisible by 1 and 3 and no other numbers, but 4 is not a prime number because it’s divisible by 2 as well as 1 and 4. You will write methods that test whether a number is positive, negative, odd, even, and divisible by another number. You will then experiment with these methods to determine if the numbers 5, 6, and 7 are prime. And to ask questions about prime numbers like whether all odd numbers are prime.

Project 2.2.6.

Complete the methods below to determine if a number is positive, negative, odd, even, or divisible by another number by returning boolean expressions testing the argument number. Then, experiment with these methods to determine if the numbers 5, 6, and 7 are prime.
With your POGIL group, use the code you completed above to experiment to see if 5, 6, and 7 are prime. Answer the following questions:

Activity 2.2.7.

Is 5 prime?
  • Yes, 5 is prime.
  • 5 is only divisible by 1 and 5, so it is prime.
  • No, 5 is not prime.
  • 5 is only divisible by 1 and 5.

Activity 2.2.8.

Is 6 prime?
  • Yes, 6 is prime.
  • A prime number is only divisible by 1 and itself. 6 is divisible by 1, 2, 3, and 6.
  • No, 6 is not prime.
  • 6 is divisible by 1, 2, 3, and 6, so it is not prime.

Activity 2.2.9.

Is 7 prime?
  • Yes, 7 is prime.
  • 7 is only divisible by 1 and 7, so it is prime.
  • No, 7 is not prime.
  • 7 is only divisible by 1 and 7.

Activity 2.2.10.

Activity 2.2.11.

Prime numbers are very useful in encryption algorithms because they can be used as keys for encoding and decoding. If you have the key, you can use it to divide a large number that represents something encrypted to decode it, but if you don’t have the key, it’s very hard to guess the factors of a large number to decode it. If you’re curious about this, watch this Numberphile video.

Subsection 2.2.5 Summary

  • (AP 2.2.A.1) Values or expressions can be compared using the relational operators == and != to determine whether the values are the same. With primitive types, this compares the actual primitive values. With reference types, this compares the object references.
  • (AP 2.2.A.2) Numeric values or expressions can be compared using the relational operators (<, >, <=, >=) to determine the relationship between the values.
  • (AP 2.2.A.3) An expression involving relational operators evaluates to a Boolean value of true or false.
  • The remainder operator % can be used to test for divisibility by a number. For example, num % 2 == 0 can be used to test if a number is even.

Subsection 2.2.6 AP Practice

Activity 2.2.12.

Consider the following statement.
boolean x = (5 % 3 == 0) == (3 > 5);
What is the value of x after the statement has been executed?
  • false
  • Although both sides of the middle == are false, false == false is true! Tricky!
  • true
  • (5 % 3 == 0) is false and (3 > 5) is false, and false == false is true! Tricky!
  • (5 % 3 == 0)
  • The boolean x should hold true or false.
  • (3 > 5)
  • The boolean x should hold true or false.
  • 2
  • The boolean x should hold true or false.

Activity 2.2.13.

Consider the following Boolean expression in which the int variables x and y have been properly declared and initialized.
(x >= 10) == (y < 12)
Which of the following values for x and y will result in the expression evaluating to true ?
  • x = 10 and y = 12
  • The left side is true, but y must be less than 12 to make the right side true.
  • x = 9 and y = 9
  • The left side is false (x must be greater than or equal to 10), but the right side is true.
  • x = 10 and y = 11
  • Correct! Both sides are true!
  • x = 10 and y = 13
  • The left side is true, but y must be less than 12 to make the right side true.
  • x = 9 and y = 12
  • Correct! Both sides are false! This is tricky!

Subsection 2.2.7 Relational Operators Practice Game

Try the game below to practice. Click on Relationals, evaluate the relational expression and click on None, All, or the numbers that make the expression true. Check on Compound for an added challenge. We encourage you to work in pairs and see how high a score you can get.
You have attempted of activities on this page.