Read Java Interview Questions and Answers for Beginners and Experienced

Java Greater than operator Example

In this Java examples series of Java tutorial, we will see Java greater than Operator with example.

 


Java Greater than operator

Java greater than operator is written as > (greater than sign). Greater than operator in Java is used to check the greater value between two variables. it returns the boolean true, if condition is true otherwise false.

 


Syntax of Java Greater than operator

Syntax of greater than operator in Java is

X > Y
  • X and Y are two variables.
  • > is greater than operator which will calculate whether X is greater than Y or not.

 


Java greater than operator Example

public class JavaExamples {

public static void main(String[] args) {
//Java Greater than operator Example in Java
int a=10;
int b=20;
System.out.println("a is greater than b \n"+(a > b));

}

}

Output

a is greater than b 
false

Explanation

In above example of Java greater than operator, there are two variable a=1o and b=20. we have to check whether a is greater than b or not. we can simply check with Java greater than operator (>). it will return true if condition is true otherwise it returns false.

In our case condition is false, because 10 is less than 20. that’s why it printed false.

 


Greater than Operator with if condition example

As greater than operator returns true or false, that’s why greater than operator can easily be used in if condition. below is the example where greater than operator is used with Java if condition.


public class JavaExamples {

public static void main(String[] args) {
//Java Greater than operator Example in Java
int a=10;
int b=20;
if(a > b) {
System.out.println("a is greater than b");
}else {
System.out.println("b is greater than a");
}

}

}

Output


b is greater than a

Explanation

As greater than operator returns true or false, that’s why we can use greater than operator in if condition. here we used > operator in if condition to evaluate whether a is greater than b or not.

In this post, we study about Java greater than operator with example and learn chow greater than operator in Java can be evaluated as true or false.