Double.equals() Method in Java with Examples
The java.lang.Double.equals() 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 Double object that contains the same double value as this object. It returns false if both the objects are not same. In all other cases, compareTo method should be preferred.
public boolean equals(Object obj)
Parameter: The method accepts only one parameter.
obj – 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 Double object that contains the same double value as this object. It returns false if the object is not same.
Below programs illustrates the use of java.lang.Double.equals() method:
Object1 & Object2: Not equal Object1 & Object2: Equal
Program 2: When no argument is passed.
prog.java:15: error: no suitable method found for equals(no arguments) if (obj1.equals()) ^ method Object.equals(Object) is not applicable (actual and formal argument lists differ in length) method Double.equals(Object) is not applicable (actual and formal argument lists differ in length) 1 error
Program 3: When anything other than the object is passed as an argument.
Object1 & Object2: Not Equal