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.
Syntax:
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:
Program 1:
import java.lang.*;
class Gfg1 {
public static void main(String args[])
{
Double obj1 = new Double( 123123 );
Double obj2 = new Double( 164165 );
System.out.print( "Object1 & Object2: " );
if (obj1.equals(obj2))
System.out.println( "Equal" );
else
System.out.println( "Not equal" );
obj1 = new Double( 12345 );
obj2 = new Double( 12345 );
System.out.print( "Object1 & Object2: " );
if (obj1.equals(obj2))
System.out.print( "Equal" );
else
System.out.print( "Not Equal" );
}
}
|
Output:
Object1 & Object2: Not equal
Object1 & Object2: Equal
Program 2: When no argument is passed.
import java.lang.Math;
class Gfg1 {
public static void main(String args[])
{
Double obj1 = new Double( 124 );
Double obj2 = new Double( 167 );
System.out.print( "Object1 & Object2: " );
if (obj1.equals())
System.out.println( "Equal" );
else
System.out.println( "Not Equal" );
}
}
|
Output:
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.
import java.lang.Math;
class Gfg1 {
public static void main(String args[])
{
Double obj1 = new Double( 124 );
System.out.print( "Object1 & Object2: " );
if (obj1.equals( "gfg" ))
System.out.println( "Equal" );
else
System.out.println( "Not Equal" );
}
}
|
Output:
Object1 & Object2: Not Equal
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Jun, 2018
Like Article
Save Article