Java Greater Than

In Java, Greater Than Relational Operator is used to check if first operand is greater than the second operand. In this tutorial, we will learn how to use the Greater Than Operator in Java, with examples.

The symbols used for Greater Than operator is >. Greater Than operator takes two operands: left operand and right operand as shown in the following.

left_operand > right_operand

The syntax to check if x is greater than y using Greater Than operator is

x > y

The operator returns a boolean value of true if x is greater than y, or false if not.

Examples

In the following example, we take two integer values in x and y, and check if the value in x is greater than that of y, using Greater Than operator.

Main.java

public class Main {
    public static void main(String[] args) {
        int x = 8;
        int y = 4;
        if (x > y) {
            System.out.println("x is greater than y.");
        } else {
            System.out.println("x is not greater than y.");
        }
    }
}

Output

x is greater than y.

Since value in x is greater than that of in y, x > y returned true.

Now, let us take values in x and y such that x is not greater than y, and observe what Greater Than operator returns for these operand values.

Main.java

public class Main {
    public static void main(String[] args) {
        int x = 8;
        int y = 9;
        if (x > y) {
            System.out.println("x is greater than y.");
        } else {
            System.out.println("x is not greater than y.");
        }
    }
}

Output

x is not greater than y.

Since value in x is not greater than that of in y, x > y returned false.

ADVERTISEMENT

Conclusion

In this Java Tutorial, we learned what Greater Than operator is, its syntax and usage in Java Programs with the help of examples.