Java Relational Operators

In this chapter you will learn:

  1. What are the relational operators in Java

Relational Operators and their operations

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

Operator Result
== 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. It compares two int values and assign the result to boolean value c.

public class Main {
  public static void main(String[] argv) {
    int a = 4;//  j  ava2 s .  c om
    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.

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));
  }/* j  a v a 2s. co m*/
}

The output generated by this program is shown here:

Next chapter...

What you will learn in the next chapter:

  1. What are Java boolean operators
  2. What are Short-Circuit Logical Operators in Java
  3. How to use the ternary ? Operator
Home » Java Tutorial » Operators Statements
Your first Java program
Your second Java program
Java Keywords and Identifiers
Variable stores value in a Java program
Variable Scope and Lifetime
Java Operators
Java Arithmetic Operators
Java Bitwise Operators
Java Relational Operators
Java Boolean Logical Operators
Java If Statement
Java switch Statement
Java while Loop
Java for loop
Java for each loop
Java break statement
Java continue statement
Java return statement
Java Comments
Java documentation comment(Javadoc)