Signup/Sign In

Java Float compare() Method

Java compare() method belongs to the Float class. This method is used to compare the value of two float values numerically to find which one is greater than the other.

The compare(float x,float y) method returns the value:

  • 0; if the value of x and y are equal.

  • -1; if the value of x is less than y.

  • 1; if the value of x is more than y.

Syntax:

public static int compare(float f1, float f2)  

Parameters:

The parameters passed consists of two float values float f1 and float f2 that represents the first and second float values to be compared respectively.

Returns:

Value 0, 1, -1 is returned depending upon the values of f1 and f2.

Example 1:

Here, four values n1, n2, n3, n4 are taken with respective float values and are checked for the values are either less, more, or equal than the compared values.

import java.lang.Float;

public class StudyTonight 
{  
    public static void main(String[] args) 
     {          
      float n1 = 100.0f;  
      float n2 = 200.0f;  
      float n3 = 300.0f;  
      float n4 = 100.0f;  
          
        System.out.println(Float.compare(n1, n2));  //Output will be less than zero
        
        System.out.println(Float.compare(n3, n1)); // Output will be greater than zero  
        
        System.out.println(Float.compare(n1,n4));  // Output will be equal to zero
      }  
}  


-1
1
0

Example 2:

Here, four float values n1, n2, n3, n4 are taken in an array and are checked for the values are either less, more, or equal when compared with the first value of the array.

import java.lang.Float;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {          
      float []n = {-100.0f,-200.0f,300.0f,-100.0f};  
      
       for(int i=0;i<4;i++)

       {
          System.out.println(Float.compare(n[0], n[i]));  
        
       }  
    } 
} 


0
1
-1
0

Example 3:

Here is a general example where the user can enter the numbers of his choice and get the desired output. The try and catch block is used for handling any exception taking place during the execution of the program. (For example, the user enters a String, etc. in place of the float value).

import java.util.Scanner; 
import java.lang.Float;
public class StudyTonight 
{  
    public static void main(String[] args) 
    {      
        Scanner sc = new Scanner(System.in);  
        System.out.print("Enter first and second number ");  
          try
          {
           float n1 = sc.nextFloat();  
           float n2 = sc.nextFloat();  
           int r =  Float.compare(n1, n2);    
             if(r > 0)
              {  
               System.out.println("first number is greater");  
              }
              else if(r< 0) 
              {  
                System.out.println("second number is greater");  
              } 
              else
              {  
               System.out.println("both numbers are equal");
              }
           }
          catch(Exception e)
           {
            System.out.println("Error!!");
           }
          
      }  
 }  


Enter first and second number 200.0 80.8
first number is greater
**************************************************
Enter first and second number 10.5 89.2
second number is greater
***************************************************
Enter first and second number 0x67 09
Error!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.