Relational Operators

The relational operators determine the relationship between two operands. The relational operators are:

OperatorResult
==Equal to
!=Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

For example, the following code fragment is perfectly valid:

  
public class Main {
  public static void main(String[] argv) {
    int a = 4;
    int b = 1;
    boolean c = a < b;

    System.out.println("c is " + c);
  }
}
  

The result of a < b (which is false) is stored in c.


c is false

The outcome of a relational operator is a boolean value.

In the following code, the System.out.println outputs the result of a relational operator.

  
public class Main {
  public static void main(String args[]) {
    // outcome of a relational operator is a boolean value
    System.out.println("10 > 9 is " + (10 > 9));
  }
}

The output generated by this program is shown here:


10 > 9 is true
Home 
  Java Book 
    Language Basics  

Operators:
  1. Operators
  2. Arithmetic Operators
  3. Bitwise Operators
  4. Relational Operators
  5. Boolean Logical Operators
  6. The ? Operator
  7. Operator Precedence