Java - Greater Than Operator >

What is Greater Than Operator?

The greater than operator (>) has the following form

operand1 > operand2 
  

The greater than operator returns true if the value of operand1 is greater than the value of operand2. Otherwise, it returns false.

The greater than operator can only be used with primitive numeric data types.

If either of the operand is NaN (float or double), it returns false.

Example

> cannot be used with boolean operands

b = (true > false); // A compile-time error. 

> cannot be used with reference type operands str1 and str2

String str1 = "Hello"; 
String str2 = "How is Java?"; 
  
b = (str1 > str2); // A compile-time error. 

The following code shows how to use Greater Than Operator.

int i = 10; 
int j = 15; 
double d1 = Double.NaN; 
boolean b; 
  
b = (i > j); // Assigns false to b 
b = (j > i); // Assigns true to b 
b = ( d1 > Double.NaN ); // Assigns false to b 

Demo

public class Main {
  public static void main(String[] args) {
    int i = 10; //from   www.  ja  v a2  s.  c  o m
    int j = 15; 
    double d1 = Double.NaN; 
    boolean b; 
      
    b = (i > j); // Assigns false to b
    System.out.println(b);
    b = (j > i); // Assigns true to b
    System.out.println(b);
    b = ( d1 > Double.NaN ); // Assigns false to b 
    System.out.println(b);
  }
}

Result

Related Topics