Comparing Values
Comparison operators evaluate a relationship between two operands and return a
booleanresult (trueorfalse). Java provides equality, relational, and type-check operators.
Equality and Inequality: == and !=
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
javaint a = 5, b = 5, c = 6;
System.out.println(a == b); // true
System.out.println(a == c); // false
System.out.println(a != c); // trueRelational Operators
| Operator | Meaning |
|---|---|
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
>= |
Greater than or equal to |
These work only on numeric types (byte, short, int, long, float, double, char).
javaSystem.out.println(3 < 5); // true
System.out.println(5 <= 5); // true
System.out.println(5 > 5); // false
System.out.println('A' < 'B'); // true — char comparison uses Unicode valuesComparing Primitives
Primitive comparison is straightforward — values are compared directly:
javaint x = 10;
int y = 10;
System.out.println(x == y); // true
double d1 = 0.1 + 0.2;
double d2 = 0.3;
System.out.println(d1 == d2); // false — floating-point precision issueAvoid
==withdoubleorfloatvalues that result from calculations. Use an epsilon comparison instead:Math.abs(d1 - d2) < 1e-9.
Comparing Objects with ==
For object references, == checks whether both variables point to the same object in memory — not whether the objects have equal content.
javaString s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); // false — different objects
System.out.println(s1.equals(s2)); // true — same contentString literals from the string pool are often the same object:
javaString a = "hello";
String b = "hello";
System.out.println(a == b); // true — both reference the same pool entry
System.out.println(a.equals(b)); // trueOn the OCP exam, watch for
==vs..equals()questions involvingString,Integer, and other wrapper types.
Integer Cache and Autoboxing
Java caches Integer objects in the range -128 to 127. == on cached values returns true; outside that range it returns false.
javaInteger i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2); // true — cached
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4); // false — not cached, different objects
System.out.println(i3.equals(i4)); // trueThe instanceof Operator
instanceof tests whether an object is an instance of a given type. It returns false (not an exception) when the reference is null.
javaObject obj = "Hello";
System.out.println(obj instanceof String); // true
System.out.println(obj instanceof Integer); // false
Object nullRef = null;
System.out.println(nullRef instanceof String); // false (no NullPointerException)Pattern Matching instanceof (Java 16+, finalized in Java 21)
Combines the type test with a binding in a single expression:
javaObject obj = "OCP";
if (obj instanceof String s) {
System.out.println(s.length()); // s is already a String — no cast needed
}instanceof with Reference Types
instanceof only applies to reference types — not primitives:
javaint x = 5;
// System.out.println(x instanceof Integer); // compile errorComparing null
javaString s = null;
System.out.println(s == null); // true
System.out.println(null == null); // true
// s.equals("test"); // throws NullPointerExceptionA safe pattern is to call equals on the known non-null value:
java"expected".equals(s); // false — no NPE even when s is nullCommon Exam Scenarios
java// Scenario 1: comparing boolean expressions
boolean b1 = true, b2 = false;
System.out.println(b1 == !b2); // true == true → true
// Scenario 2: char vs int
char c = 'A'; // Unicode 65
System.out.println(c == 65); // true
// Scenario 3: promotion in comparison
int i = 1000000;
long l = 1000000L;
System.out.println(i == l); // true — i promoted to long
// Scenario 4: wrapper vs primitive
Integer w = 5;
int p = 5;
System.out.println(w == p); // true — w unboxed to int before comparisonKey Rules to Remember
==on objects tests reference identity, not content equality — use.equals()for content.null instanceof AnyTypealways returnsfalse— never throws.- Integer autoboxing cache covers -128 to 127;
==onIntegerobjects outside that range returnsfalse. - When comparing a wrapper type to a primitive with
==, the wrapper is unboxed first. - Floating-point
==comparisons are unreliable due to precision; prefer an epsilon check.
References
- Oracle Docs — Equality, Relational, and Conditional Operators
- OCP Oracle Certified Professional Java SE 21 Study Guide — Chapter 2