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.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// of java.lang.Double.equals() method
import java.lang.*;
  
class Gfg1 {
  
    public static void main(String args[])
    {
  
        // When two objects are different
        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");
  
        // When two objects are 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");
    }
}

chevron_right


Output:

Object1 & Object2: Not equal
Object1 & Object2: Equal

Program 2: When no argument is passed.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// of java.lang.Double.equals() method
import java.lang.Math;
  
class Gfg1 {
  
    // Driver code
    public static void main(String args[])
    {
  
        // When no argument is passed
        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");
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// of java.lang.Double.equals() method
import java.lang.Math;
  
class Gfg1 {
  
    // Driver code
    public static void main(String args[])
    {
  
        // When anything other than the argument is passed
        Double obj1 = new Double(124);
  
        System.out.print("Object1 & Object2: ");
        if (obj1.equals("gfg"))
            System.out.println("Equal");
        else
            System.out.println("Not Equal");
    }
}

chevron_right


Output:

Object1 & Object2: Not Equal


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.