skip to content

Relational & Equality Operators

Relational operators compare primitives numerically, while == on references compares identity, not contents — hence equals. Interviewers reach for the string-comparison case to see whether you can explain the interning behavior behind it.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What are Java's relational and equality operators, and what do they evaluate to?

level: juniorimportance: must knowfreq 70%

answer

  1. <, <=, >, >= are relational; == and != are equality
  2. Result is always a boolean, never an int
  3. Relational = numeric/char only; equality also covers references and boolean
  4. if/while conditions require a boolean

basics

~10 s

Relational operators are <, <=, >, >= and equality operators are == and !=. They compare two values and always produce a boolean: true or false.

solid answer

~40 s

Java 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

for a junior

List the six operators and state that each yields true or false.

for a middle

Distinguishes relational (numeric/char only) from equality (also references and boolean) and explains the boolean-not-int result.

for a senior

Connects the boolean result to type-safety of conditions and ties equality's reference behavior to the primitive/object model.

for a principal

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

context

open as a page

How do == and the relational operators behave when comparing primitive values, including mixed numeric types?

level: middleimportance: must knowfreq 60%

basics

~20 s

For primitives, == checks if the two values are equal and the relational operators compare their size. If the two operands are different number types, the smaller one is converted up to the larger type before comparing.

open as a page

What is the difference between == and .equals() when comparing objects in Java?

level: middleimportance: must knowfreq 95%

basics

~20 s

For objects, == checks whether two variables point to the very same object (identity). .equals() checks whether two objects are meaningfully equal in content. Use .equals() for value comparison, like comparing the text of two Strings.

open as a page

Why can == sometimes return true and sometimes false when comparing two Integer objects with the same value?

level: seniorimportance: should knowfreq 55%

basics

~20 s

Integer is an object, so == compares references, not the number. Java caches small boxed values (-128 to 127), so two boxed ints in that range can share one object and be == ; outside that range they are different objects and == is false. Always use .equals() or unbox to int.

open as a page

When ordering objects, why can't you use relational operators, and what do you use instead?

level: seniorimportance: should knowfreq 45%

basics

~10 s

Relational operators (<, >) only work on numbers, not objects. To order objects you implement Comparable's compareTo (or pass a Comparator) and check whether its result is negative, zero, or positive.

open as a page