New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs

Java Float equals() Method

Java equals() method belongs to the Float class. This method is used to compare the value of the Float object currently used with the value of the parameter. It has boolean as its return type i.e returns true if the value of the Float object is equal to the value of the parameter and returns false if the value of the Float object is not equal to the value of the parameter.

This method overrides the equals() method of Object class.

Syntax:

public boolean equals(Object obj)

Parameter:

The parameter passed is the Object which is to be checked for equality with the Float object.

Returns:

Returns true if the value of Float object value is equal to the value passed as a parameter and returns false if the value of Float object value is not equal to the value passed as parameter.

Example 1:

Here the Float objects ob1, ob2, ob3 are checked for equality with the help of equals() method.

import java.lang.Float;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {          
        Float ob1 = 20.0f; 
        Float ob2 = 40.0f; 
        Float ob3 = 40.0f;  
          
         //Checking for objects with equal and unequal values
        System.out.println("ob1 and ob2 equal?  "+ob1.equals(ob2));  
        System.out.println("ob2 and ob3 equal?  "+ob2.equals(ob3));
         
                          
    }  
} 


ob1 and ob2 equal? false
ob2 and ob3 equal? true

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the desired result.

import java.util.Scanner; 

public class StudyTonight 
{  
    public static void main(String[] args)
    {          
        try
        {
          Scanner sc = new Scanner(System.in);  
          System.out.print("Enter first float value: ");  
          Float n1 = sc.nextFloat();  
          System.out.print("Enter second float value ");  
          Float n2 = sc.nextFloat();  
          boolean r = n1.equals(n2);  
            if (r==true)
             {               
               System.out.println("Same numbers entered");  
             }  
           else
             {  
               System.out.println("Different numbers entered");  
             }
        }
        catch(Exception e)
        {
          System.out.println("Invalid input!!Please check");
        }
    }  
}  


Enter first float value: 67.98
Enter second float value 87.09
Different numbers entered
************************************
Enter first float value: 32.22
Enter second float value 32.22
Same numbers entered
*************************************
Enter first float value: 0x87
Invalid input!!Please check

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.