In this Java examples series of Java tutorial, we will see Java Less than Operator with example.
Java Less than operator
Java Less than operator is written as < (Less than sign). Less than operator in Java is used to check the Less value between two variables. it returns the boolean true, if condition is true otherwise false.
Syntax of Java Less than operator
Syntax of Less than operator in Java is
X < Y
- X and Y are two variables.
- < is Less than operator which will calculate whether X is Less than Y or not.
Java Less than operator Example
public class JavaExamples { public static void main(String[] args) { //Java Less than operator Example in Java int a=10; int b=20; System.out.println("a is Less than b \n"+(a < b)); } }
Output
a is Less than b true
Explanation
In above example of Java Less than operator, there are two variable a=1o
and b=20
. we have to check whether a is Less than b or not. we can simply check with Java Less than operator (<). it will return true
if condition is true otherwise it returns false
.
In our case condition is true, because 10 is less than 20. that’s why it printed true.
Less than Operator with if condition example
As Less than operator returns true or false, that’s why Less than operator can easily be used in if condition. below is the example where Less than operator is used with Java if condition.
public class JavaExamples { public static void main(String[] args) { //Java Less than operator Example in Java int a=10; int b=20; if(a < b) { System.out.println("a is Less than b"); }else { System.out.println("b is Less than a"); } } }
Output
b is Less than a
Explanation
As Less than operator returns true or false, that’s why we can use Less than operator in if condition. here we used < operator in if condition to evaluate whether a is Less than b or not.
In this post, we study about Java Less than operator with example and learn chow Less than operator in Java can be evaluated as true or false.