Table of Contents

    Java Relational Operators: Examples and Usage

    Java Relational Operators: Examples and Usage

    Relational (Comparison) Operators

    These operators are used to compare two values. They return a boolean value (true or false).

    Operator Description Example
    == Equal to a == b
    != Not equal to a != b
    > Greater than a > b
    < Less than a < b
    >= Greater than or equal to a >= b
    <= Less than or equal to a <= b

    Below program is a simple example which demonstrates the relational operators. Copy and paste the following Java program in RelationalOperator.java file, and compile and run this program

    Arithmetic Operators

    public class RelationalOperator {
    
       public static void main(String args[]) {
          int p = 5;
          int q = 10;
    
          System.out.println("p == q = " + (p == q) );
          System.out.println("p != q = " + (p != q) );
          System.out.println("p > q = " + (p > q) );
          System.out.println("p < q = " + (p < q) );
          System.out.println("q >= p = " + (q >= p) );
          System.out.println("q

    Output

    p == q = false
    p != q = true
    p > q = false
    p < q = true
    q >= p = true
    q