What are Java's relational and equality operators, and what do they evaluate to?
answer
- <, <=, >, >= are relational; == and != are equality
- Result is always a boolean, never an int
- Relational = numeric/char only; equality also covers references and boolean
- if/while conditions require a boolean
basics
~10 sRelational operators are <, <=, >, >= and equality operators are == and !=. They compare two values and always produce a boolean: true or false.
solid answer
~40 sJava has four relational operators (<, <=, >, >=) and two equality operators (== and !=). Every one of them is a binary operator that takes two operands and evaluates to a boolean (true or false), never an int like in C. The relational operators only apply to numeric primitive types (and char), comparing magnitude. The equality operators are broader: == and != work on numeric primitives, char, boolean, and also on object references. For primitives they compare values; for objects they compare references (identity), not contents. Because the result is a strict boolean, you can use these directly in if, while, and ternary conditions without a separate comparison to zero.
go deeper
List the six operators and state that each yields true or false.
Distinguishes relational (numeric/char only) from equality (also references and boolean) and explains the boolean-not-int result.
Connects the boolean result to type-safety of conditions and ties equality's reference behavior to the primitive/object model.
Frames it within Java's deliberate type safety choices (no truthiness) and discusses how this prevents whole classes of bugs versus C-style integer truth values.
## What these operators are An **operator** is a symbol that performs an operation on one or more **operands** (the values it works on). A **binary operator** takes exactly two operands, one on each side, e.g. `a < b`. Java splits comparison into two groups: - **Relational operators** — `<` (less than), `<=` (less than or equal), `>` (greater than), `>=` (greater than or equal). They answer *which is bigger*. - **Equality operators** — `==` (equal to) and `!=` (not equal to). They answer *are these the same*. ## They always return a boolean The result of any of these operators is a value of type `boolean`, which has exactly two possible values: `true` and `false`. This is different from languages like C, where a comparison yields an integer (1 or 0). In Java you cannot write `if (5)` — the condition of an `if`, `while`, or the first part of a ternary `?:` must be a boolean, so it must come from something like a comparison. Example: ```java int a = 3, b = 7; boolean smaller = a < b; // true boolean equal = a == b; // false ``` ## What each group can compare - **Relational operators** apply only to **numeric primitive types** — `byte`, `short`, `int`, `long`, `float`, `double` — and to `char` (which is numeric: it holds a Unicode code unit). You cannot use `<` on `boolean` or on object references; that is a compile error. - **Equality operators** are wider. They work on numeric primitives, `char`, `boolean`, and on **object references**. For primitives they compare the actual values; for references they compare identity (whether both refer to the same object). More on that distinction lives in the dedicated questions. ## A **primitive** vs an **object** A **primitive type** stores a raw value directly (e.g. an `int` variable holds the number itself). An **object** lives on the heap, and a variable holds a **reference** (a handle/pointer) to it. This split is the whole reason `==` behaves in two different ways, covered separately. ## Why it matters These operators are the backbone of every condition, loop guard, and branch. Knowing they return a boolean (so they compose cleanly with `&&`, `||`, `!`) and knowing relational is numeric-only while equality also covers references is the foundation for avoiding the classic `==` vs `.equals()` bug.
- Can you use < to compare two String objects?No. Relational operators are numeric-only, so a < on String is a compile error. Use s1.compareTo(s2) < 0 for ordering instead.
- Why can't you write if (someInt) in Java?Because if requires a boolean and an int is not implicitly convertible to boolean. You must write an explicit comparison such as if (someInt != 0).
saying these in an interview costs you the question
- Saying a comparison returns 1 or 0 (that is C, not Java)
- Claiming you can use < on objects or booleans
- Thinking == only works on numbers