Double.equals() Method in Java with Examples

19 Jun, 2018
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:
// Java program to demonstrate// of java.lang.Double.equals() methodimport 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");    }}
Object1 & Object2: Not equal
Object1 & Object2: Equal

Program 2: When no argument is passed.
// Java program to demonstrate// of java.lang.Double.equals() methodimport 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");    }}
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.
// Java program to demonstrate// of java.lang.Double.equals() methodimport 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");    }}
Object1 & Object2: Not Equal

Java-Functions
Java-lang package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
ArrayList in Java
Collections in Java
Multidimensional Arrays in Java
Stream In Java
Set in Java
Singleton Class in Java
Stack Class in Java
Initialize an ArrayList in Java
Initializing a List in Java