Java Less Than or Equal To
In Java, Less Than or Equal To Relational Operator is used to check if first operand is less than or equal to the second operand. In this tutorial, we will learn how to use the Less Than or Equal To Operator in Java, with examples.
The symbols used for Less Than or Equal To operator is <=
. Less Than or Equal To 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 less than or equal to y
using Less Than or Equal To operator is
x <= y
The operator returns a boolean value of true
if x
is less than or equal to y
, or false
if not.
The following table gives the return value of Less Than or Equal to operator for some operand values.
x | y | x <= y |
---|---|---|
4 | 8 | true |
4 | 4 | true |
9 | 2 | false |
Examples
In the following example, we take two integer values in x
and y
, and check if the value in x
is less than or equal to y
, using Less Than or Equal To operator.
Main.java
public class Main {
public static void main(String[] args) {
int x = 4;
int y = 8;
if (x <= y) {
System.out.println("x is less than or equal to y.");
} else {
System.out.println("x is not less than or equal to y.");
}
}
}
Output
x is less than or equal to 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 or equal to y
, and observe what Less Than or Equal To operator returns for these operand values.
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 less than or equal to y.");
} else {
System.out.println("x is not less than or equal to y.");
}
}
}
Output
x is not less than or equal to y.
Since value in x
is not less than or equal to that of in y
, x <= y
returned false.
Conclusion
In this Java Tutorial, we learned what Less Than or Equal To operator is, its syntax and usage in Java Programs with the help of examples.