Java Less Than

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

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

</>
Copy
left_operand < right_operand

The syntax to check if x is less than y using Less Than operator is

</>
Copy
x < y

The operator returns a boolean value of true if x is less 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 less than that of y, using Less Than operator.

Main.java

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

Output

x is less than y.

Since value in x is less than that of in y, x < y returned true.

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

Main.java

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

Output

x is not less than y.

Since value in x is not less than that of in y, x < y returned false.

Conclusion

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