Java Relational Operators With Example

Relational operators are used to check the relationship between two operands. For example,

// check if a is less than b

A > B;

Here, > operator is the relational operator. It checks if A is greater than B or not.

 

Operator

Description

Example

==

Is Equal To

3 == 5 returns false

!=

Not Equal To

3 != 5 returns true

> 

Greater Than

3 > 5 returns false

< 

Less Than

3 < 5 returns true

>=

Greater Than or Equal To

3 >= 5 returns false

<=

Less Than or Equal To

3 <= 5 returns true

 

Example Java Relational Operators

    // create variables

    int a = 7, b = 11;

 

    // value of a and b

    System.out.println("a is " + a + " and b is " + b);

 

    // == operator

    System.out.println(a == b);  // false

 

    // != operator

    System.out.println(a != b);  // true

 

    // > operator

    System.out.println(a > b);  // false

 

    // < operator

    System.out.println(a < b);  // true

 

    // >= operator

    System.out.println(a >= b);  // false

 

    // <= operator

    System.out.println(a <= b);  // true