dev only

Boolean Expressions

Relational Operators

Six operators compare values and always produce true or false:

==   // equals
!=   // does not equal
<    // less than
>    // greater than
<=   // less than or equal to
>=   // greater than or equal to
double battery = 11.8;
System.out.println(battery < 12.0);  // true — low battery
System.out.println(battery >= 12.0); // false

Equality: Primitives vs. Objects

== on primitives (int, double, boolean) compares the actual values — exactly what you'd expect. == on objects compares whether two variables refer to the same object in memory, not whether their contents look the same. Two separately-built TalonFX objects with identical settings are still two different objects — == between them is false. (Object equality gets its own full treatment in Ch.6, once Strings are introduced.)

Divisibility with the Remainder Operator

% (from Ch.2) is how you test whether a number divides evenly into another:

(number % 2 == 0)  // true if number is even
(number % 2 != 0)  // true if number is odd
(number % x == 0)  // true if number is evenly divisible by x

Common Pitfalls

  • Using == on objects expecting a content comparison. It checks "same object," not "same values" — two different but identical-looking objects compare unequal.
  • Testing oddness with num % 2 == 1. This looks right but fails for negative odd numbers (their remainder is -1, not 1). Use num % 2 != 0 instead — it works for both signs.

Key Takeaways

  • Relational operators (== != < > <= >=) always produce a boolean result.
  • == on primitives compares values; == on objects compares identity (same object), not content.
  • % tests divisibility — num % 2 == 0 for even, num % 2 != 0 for odd (not == 1, which breaks on negatives).

Derived from csawesome-2026: unit-2-selection-and-iteration/2.02-boolean-expressions.md (CSA 2.2)