The equals() method in Float Class is a built-in function in java that compares this object to the specified object. The result is true if and only if the argument is not null and is a Float object that contains the same double value as this object. It returns false if both the objects are not same.
Syntax:
public boolean equals(Object obj)
Parameter: The method accepts only one parameter obj which specifies the passed object is the object that is to be compared with.
Return Values: The function returns a boolean value after comparing with the object passed in the parameter:
- It returns true if and only if the argument is not null and is a Float object that contains the same double value as this object. It returns false if the object is not same.
- If f1 and f2 both is represented as Float.NaN, then the equals() method returns true, even if Float.NaN==Float.NaN has the value false.
- If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.
Below programs illustrates the use of Float.equals() method:
Program 1:
// Java program to demonstrate // Float.equals() method   import java.lang.*;   class Gfg1 {       public static void main(String args[])     {           // When two objects are different         Float obj1 = new Float( 123123 );         Float obj2 = new Float( 164165 );           System.out.print( "The objects " + obj1                          + " and " + obj2                          + "are : " );         if (obj1.equals(obj2))             System.out.println( "Equal" );         else             System.out.println( "Not equal" );           // When two objects are equal         obj1 = new Float( 12345 );         obj2 = new Float( 12345 );         System.out.print( "The objects " + obj1                          + " and " + obj2                          + "are : " );         if (obj1.equals(obj2))             System.out.print( "Equal" );         else             System.out.print( "Not Equal" );     } } |
The objects 123123.0 and 164165.0are : Not equal The objects 12345.0 and 12345.0are : Equal
Program 2: Using Float.NaN
// Java program to demonstrate // Float.equals() method   import java.lang.*;   class Gfg1 {       public static void main(String args[])     {           Float obj1 = new Float(Float.NaN);         Float obj2 = new Float(Float.NaN);           System.out.print( "The objects " + obj1                          + " and " + obj2                          + "are : " );         if (obj1.equals(obj2))             System.out.println( "Equal" );         else             System.out.println( "Not equal" );     } } |
The objects NaN and NaNare : Equal
Program 3: Using float value 0.0f
// Java program to demonstrate // Float.equals() method   import java.lang.*;   class Gfg1 {       public static void main(String args[])     {           Float obj1 = new Float( 0 .0f);         Float obj2 = new Float(- 0 .0f);           System.out.print( "The objects " + obj1                          + " and " + obj2                          + "are : " );         if (obj1.equals(obj2))             System.out.println( "Equal" );         else             System.out.println( "Not equal" );     } } |
The objects 0.0 and -0.0are : Not equal
Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#equals(java.lang.Object)