UNIT 2: SELECTION AND ITERATION · TOPIC 2.2
2.2 Boolean Expressions
Expressions that evaluate to true or false control everything in this unit. The == vs .equals distinction is where points are lost.
What you need to know
- Relational operators:
==,!=,<,>,<=,>=. They produce a boolean. ==compares values for primitives and references for objects. To compare object contents (Strings especially) use.equals().- A boolean variable can hold the result:
boolean ok = score >= 70;. Thenif (ok)— no need forif (ok == true). - Arithmetic is evaluated before relational operators:
x + 1 > y * 2computes both sides first. - Comparing doubles with
==is unreliable due to round-off; the exam usually avoids it or uses a tolerance. !=means "not equal"; for Strings, use!a.equals(b).
Worked example
int a = 5, b = 10;
boolean p = a * 2 == b; // true
boolean q = a != b; // true
String s1 = "hi";
String s2 = new String("hi");
boolean r = s1 == s2; // false (different objects)
boolean t = s1.equals(s2); // true (same contents)
Exam tip: Any time a question compares two Strings with
==, be suspicious. If both came from literals it may happen to be true, but the exam's intended answer is that == is not a reliable content comparison — .equals is.Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- For primitives,
==and!=compare values.5 == 5is true. For doubles, exact equality is unreliable due to representation error; compare with a tolerance (Math.abs(a - b) < 0.0001) in real code. The exam avoids the issue. - For references,
==compares addresses. Two separate objects are never==even with identical contents. Use.equalsfor content comparison — String defines it by contents; your own classes inherit Object's version (address comparison) unless you write your own. - Relational operators have lower precedence than arithmetic, so
a + b > c * ddoes both computations first. They have higher precedence than logical operators (&&,||), soa > b && c < dcompares first, then ands. - A relational expression is a boolean value — it can be stored, returned, passed as an argument.
return x > 0;is cleaner thanif (x > 0) return true; else return false;and the exam accepts both. !=on booleans is XOR:a != bis true when exactly one is true.- Comparing a boolean to
true(if (done == true)) is redundant;if (done)means the same. Comparing tofalseisif (!done).
Mistakes that cost points
- String comparison with
==. The most common wrong answer in Unit 2. Always.equals. - Chaining comparisons.
1 < x < 10doesn't compile (the first comparison yields a boolean, which can't be compared to 10). Write1 < x && x < 10. - Using
=in a condition. Compile error for non-booleans.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Which of the following expressions correctly tests whether the String
name holds the text "Ram"?Show answer
Answer: B. equals compares contents. == compares references; = is assignment; compareTo returns an int, not a boolean.
Q2 What is the value of
x > 3 == y < 2 when x = 5 and y = 1?Show answer
Answer: A. x > 3 is true; y < 2 is true; true == true is true. (Comparing booleans with == is legal.)
Key vocabulary
- Relational operator
- <, >, <=, >=, ==, != — compares two values and yields a boolean
- equals()
- compares object contents