Java ProgramsBasics & I/ODemonstrate Relational Operators

Demonstrate Relational Operators in Java

beginner·  Basics & I/O  ·  Operators

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.

Input
10, 20
Output
10 > 20 = false

Java Program

Java
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

10 > 20 = false 10 < 20 = true 10 >= 20 = false 10 <= 20 = true 10 == 20 = false 10 != 20 = true

Core Logic

Applying all six relational operators to the same pair of ints shows how each one answers a different comparison question.

How It Works
  1. 1a holds 10 and b holds 20 throughout.
  2. 2a > b and a < b test strict greater-than and less-than.
  3. 3a >= b and a <= b test the same comparisons but also accept equality.
  4. 4a == b tests whether the two values are equal, and a != b tests whether they're different.
  5. 5Every one of these six expressions evaluates to a boolean, printed directly.
For 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

> operator< operator>= operator<= operator== operator!= operator

Approach 2: Java 8

Java
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

10 > 20 = false 10 < 20 = true 10 >= 20 = false 10 <= 20 = true 10 == 20 = false 10 != 20 = true

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.

How It Works
  1. 1A LinkedHashMap<String, BiPredicate<Integer, Integer>> holds one entry per comparison symbol, in the same order they should print.
  2. 2Each lambda — (x, y) -> x > y and so on — captures exactly one comparison as a reusable value instead of an inline expression.
  3. 3A single loop applies every comparison to the same a and b, calling test(a, b) on each.
  4. 4A LinkedHashMap preserves insertion order, so the six lines print in the same order the direct version does.
  5. 5The == and != entries use x.equals(y) rather than x == y, because BiPredicate<Integer, Integer> compares boxed Integer objects, and == on boxed types compares references, not values.
The loop applies each stored predicate to 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.

Key Concepts

BiPredicateLinkedHashMapfunctional interface

Related Programs