JavaCore java

Relational Operator in Java

What are Relational Operators?

  • Used to compare two values.
  • Result is always a boolean (true or false).

List of Relational Operators

OperatorMeaningExample (x=6, y=5)Result
<Less thanx < y6 < 5false
>Greater thanx > y6 > 5true
<=Less than or equal tox <= y6 <= 5false
>=Greater or equal tox >= y6 >= 5true
==Equal tox == y6 == 5false
!=Not equal tox != y6 != 5true

Example Program

public class Hello {
    public static void main(String[] args) {
        int x = 6;
        int y = 5;

        boolean r1 = x < y;
        boolean r2 = x >= y;
        boolean r3 = x != y;
        boolean r4 = (x == y);

        System.out.println("x < y : " + r1);
        System.out.println("x >= y: " + r2);
        System.out.println("x != y: " + r3);
        System.out.println("x == y: " + r4);
    }
}

Key Notes:

  • Relational operators are mostly used in conditions (like if statements, loops).
  • Always return true or false.
  • == checks value equality, not assignment.