Float equals() method in Java with examples
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.
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:
The objects 123123.0 and 164165.0are : Not equal The objects 12345.0 and 12345.0are : Equal
Program 2: Using Float.NaN
The objects NaN and NaNare : Equal
Program 3: Using float value 0.0f
The objects 0.0 and -0.0are : Not equal
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.