Skip to main content

Unit 2 · Topic 2.2

2.2 Boolean Expressions

A Boolean expression is a question your code asks that has a true or false answer. This topic covers the six relational operators that build those questions, and the important difference between comparing primitive values and comparing object references.

Key terms

  • Boolean expression
  • relational operator
  • == vs !=
  • boolean

The relational operators

A relational operator compares two values and produces a boolean. Every if statement and loop you write depends on one of these.

OperatorMeansExample that is true
==is equal to4 == 4
!=is not equal to4 != 5
<is less than3 < 8
>is greater than8 > 3
<=is less than or equal to5 <= 5
>=is greater than or equal to9 >= 2

How they're evaluated

Arithmetic happens before comparing. In a + 3 >= b, Java first works out a + 3, then compares the result with b. You can add parentheses to make this clearer, but you don't need them.

You can compare an int with a double. Java widens the int to a double first, so 7 == 7.0 is true.

The result of a comparison is a boolean value, so you can store it in a boolean variable, print it, or return it from a method. Writing return score >= 70; is shorter than an if statement that returns true or false.

Two common slips: = is assignment, not comparison, so always use == in a condition. And you can't chain comparisons like you do in math. 1 < x < 10 doesn't compile, because 1 < x is a boolean and you can't ask whether a boolean is less than 10. You'll learn the right way, x > 1 && x < 10, in 2.5.

Primitives versus references

With primitive types (int, double, boolean), == and != compare the actual values.

With reference types, a variable holds a reference to an object (1.12). So == asks a different question: do these two variables refer to the very same object? Two separate objects can hold the same data and still make == false.

String s1 = new String("go"); String s2 = new String("go"); String s3 = s1; System.out.println((s1 == s2) + " " + (s1 == s3));

s1 and s2 were each made with new, so they're different objects with the same letters, and s1 == s2 is false. s3 was assigned s1, so both refer to one object, and s1 == s3 is true. To compare the contents of strings, use equals (1.15 and 2.6).

The operators <, >, <= and >= only work on numbers. To put strings in order, use compareTo.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Evaluating comparisons

    What does this code print?int a = 7; int b = 10; double c = 7.0; boolean r1 = a < b; boolean r2 = a == c; boolean r3 = b % 2 != 0; boolean r4 = a + 3 >= b; System.out.println(r1 + " " + r2 + " " + r3 + " " + r4);

    Show the solution
    1. Step 1: r1: is 7 less than 10? Yes, true.
    2. Step 2: r2: a is an int and c is a double. The 7 is widened to 7.0, and 7.0 equals 7.0, so true.
    3. Step 3: r3: b % 2 is 10 % 2, which is 0. Is 0 not equal to 0? No, so false. (This expression tests whether b is odd.)
    4. Step 4: r4: arithmetic first, 7 + 3 is 10. Is 10 greater than or equal to 10? Yes, true.

    Answer: It prints true true false true.

  2. Example 2

    Spotting the error

    A student wants to check whether x is between 1 and 10, so they write:int x = 5; boolean ok = 1 < x < 10;What happens?

    Show the solution
    1. Step 1: Java evaluates 1 < x first, which gives a boolean (here, true).
    2. Step 2: The next step would be true < 10, but < can't compare a boolean with a number.
    3. Step 3: So the compiler rejects the line. This is a syntax error, caught before the program runs.

    Answer: It doesn't compile. Write x > 1 && x < 10 instead (see 2.5).

Common mistakes

  • Using = instead of == in a condition. = stores a value; == compares.
  • Comparing strings or other objects with == when you mean "same contents". Use equals.
  • Writing 1 < x < 10 like in math. Java needs two comparisons joined with &&.

On the exam

  • Many multiple-choice questions turn on one comparison: < versus <= decides whether the boundary value counts. Test the exact boundary value in your head.
  • In free-response answers, returning a comparison directly (return count > 0;) is correct and saves time.

Connected topics

Videos

Check yourself

3 questions on 2.2 Boolean Expressions. Pick an answer to see if you got it, and why.

Question 1 of 3

Consider the following code segment.int a = 7; int b = 3; boolean p = a % b == 1; boolean q = a / b >= 2.5; System.out.println(p + " " + q);What is printed as a result of executing the code segment?

Question 2 of 3

Assume n is an int variable with a positive value. Which of the following expressions evaluates to true exactly when n is a multiple of 3?

Question 3 of 3

Consider the following class.public class Counter { private int value; public Counter(int v) { value = v; } public int getValue() { return value; } }What is printed as a result of executing the following code segment?Counter a = new Counter(5); Counter b = new Counter(5); Counter c = a; System.out.println((a == b) + " " + (a == c));

0 of 3 answered