Relational Operators: ==, !=, <, >, <=, >=
What you'll learn: How to compare two values in Java using the six relational operators that return boolean results.
The Core Idea
Relational operators let you ask questions about how two values relate to each other. Every comparison produces a boolean answer: either true or false. Think of them like a referee making yes-or-no calls: "Is this number bigger than that one?" or "Are these two values equal?"
The Six Relational Operators
Java provides six comparison operators:
==— equal to: Are the values exactly the same?!=— not equal to: Are the values different?<— less than: Is the left value smaller?>— greater than: Is the left value larger?<=— less than or equal to: Is the left value smaller or the same?>=— greater than or equal to: Is the left value larger or the same?
How They Work
int age = 25;
int minAge = 18;
boolean isAdult = age >= minAge; // true (25 is >= 18)
boolean isTeen = age < 20; // false (25 is not < 20)
boolean exactMatch = age == 25; // true (25 equals 25)
boolean different = age != minAge; // true (25 is not equal to 18)
Each operator evaluates the relationship and stores a boolean result. You can use these expressions directly in assignments, or later in control structures (though you haven't learned those yet).
Common Use Cases
These operators work with all numeric types you've learned: int, long, short, byte, float, double, and even char (since characters have numeric values).
double price = 19.99;
boolean isAffordable = price <= 20.0; // true
char grade = 'B';
boolean isPassing = grade >= 'C'; // true ('B' comes before 'C' in Unicode)
Key Takeaway: Relational operators compare two values and always produce a boolean result (true or false), enabling you to test relationships like equality, inequality, and ordering.