Demonstrate Relational Operators in Java
Problem
Relational operators (> < >= <= == !=) compare two values and always produce a boolean — true or false — rather than a number.
Given two integers, print the result of comparing them with each relational operator.
Java Program
public class DemonstrateRelationalOperators {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a + " > " + b + " = " + (a > b));
System.out.println(a + " < " + b + " = " + (a < b));
System.out.println(a + " >= " + b + " = " + (a >= b));
System.out.println(a + " <= " + b + " = " + (a <= b));
System.out.println(a + " == " + b + " = " + (a == b));
System.out.println(a + " != " + b + " = " + (a != b));
}
}Output
Core Logic
Applying all six relational operators to the same pair of ints shows how each one answers a different comparison question.
- 1
aholds10andbholds20throughout. - 2
a > banda < btest strict greater-than and less-than. - 3
a >= banda <= btest the same comparisons but also accept equality. - 4
a == btests whether the two values are equal, anda != btests whether they're different. - 5Every one of these six expressions evaluates to a
boolean, printed directly.
a = 10 and b = 20: a > b is false, a < b is true, and a == b is false since the values differ.Key Point: == compares primitive values directly, but for object types like String it compares references instead of contents — that's a different rule from the primitive int comparison shown here.
Key Concepts
Approach 2: Java 8
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiPredicate;
public class RelationalOperatorsTable {
public static void main(String[] args) {
int a = 10;
int b = 20;
// Each comparison is stored as a named lambda, in the order it should print
Map<String, BiPredicate<Integer, Integer>> operators = new LinkedHashMap<>();
operators.put(">", (x, y) -> x > y);
operators.put("<", (x, y) -> x < y);
operators.put(">=", (x, y) -> x >= y);
operators.put("<=", (x, y) -> x <= y);
operators.put("==", (x, y) -> x.equals(y)); // boxed Integer: == would compare references, not values
operators.put("!=", (x, y) -> !x.equals(y));
for (Map.Entry<String, BiPredicate<Integer, Integer>> entry : operators.entrySet()) {
boolean result = entry.getValue().test(a, b);
System.out.println(a + " " + entry.getKey() + " " + b + " = " + result);
}
}
}
Output
Core Logic
Storing each comparison as a named BiPredicate in a map turns the six separate expressions into data that can be looped over and applied uniformly.
- 1A
LinkedHashMap<String, BiPredicate<Integer, Integer>>holds one entry per comparison symbol, in the same order they should print. - 2Each lambda —
(x, y) -> x > yand so on — captures exactly one comparison as a reusable value instead of an inline expression. - 3A single loop applies every comparison to the same
aandb, callingtest(a, b)on each. - 4A
LinkedHashMappreserves insertion order, so the six lines print in the same order the direct version does. - 5The
==and!=entries usex.equals(y)rather thanx == y, becauseBiPredicate<Integer, Integer>compares boxedIntegerobjects, and==on boxed types compares references, not values.
10 and 20 in turn, printing the same six lines the direct version prints inline.Key Point: BiPredicate<Integer, Integer> is the two-argument counterpart to Predicate<T> — reach for it whenever a comparison between two values needs to be passed around as a value rather than written inline.