Java - Relational Operators

Java

In this tutorial we will learn about relational operators in Java programming language.

There are 6 relational operators in Java given below.

OperatorDescription
<Is less than
<=Is less than or equal to
>Is greater than
>=Is greater than or equal to
==Is equal to
!=Is not equal to

We use relational operators to compare two values. If the condition is satisfied then we get true. Otherwise, false.

The six relational operators also forms complement as shown below.

OperatorIs Complement of
<=
>=
==!=

Is less than

In the following example we will get true because a < b.

class Relational {
  public static void main (String[] args) {
    int a = 5;
    int b = 10;
    boolean result = a < b;
    System.out.println("Result: " + result);
  }
}

Output:

Result: true

Is less than or equal to

In the following example we will get true if a <= b.

class Relational {
  public static void main (String[] args) {
    int a = 5;
    int b = 10;
    boolean result = a <= b;
    System.out.println("Result: " + result);
  }
}

Output:

Result: true

Is greater than

In the following example we will get true if a > b.

class Relational {
  public static void main (String[] args) {
    int a = 15;
    int b = 10;
    boolean result = a > b;
    System.out.println("Result: " + result);
  }
}

Output:

Result: true

Is greater than or equal to

In the following example we will get true if a >= b.

class Relational {
  public static void main (String[] args) {
    int a = 15;
    int b = 10;
    boolean result = a >= b;
    System.out.println("Result: " + result);
  }
}

Output:

Result: true

Is equal to

In the following example we will get true if a == b.

class Relational {
  public static void main (String[] args) {
    int a = 15;
    int b = 15;
    boolean result = a == b;
    System.out.println("Result: " + result);
  }
}

Output:

Result: true

Is not equal to

In the following example we will get true if a != b.

class Relational {
  public static void main (String[] args) {
    int a = 25;
    int b = 15;
    boolean result = a != b;
    System.out.println("Result: " + result);
  }
}

Output:

Result: true

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT