Open In App

Java Double.equals() Method

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Double.equals() in Java is a built-in function from the java.lang.Double class. This method compares the content of two Double objects. 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. This method is useful for checking equality, while the compareTo method is better for ordering or sorting purposes.

Syntax of Double.equals() Method

public boolean equals(Object obj)

  • Parameter: obj: This is the object to be compared.
  • Return Values: The function returns a boolean value after comparing it 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 the same.

Examples of Java Double.equals() Method

Example 1: In this example, we are going to compare two Double objects.

Java
// Java program to demonstrate 
// Double.equals() with valid comparisons
import java.lang.Double;

public class Geeks {

    public static void main(String[] args) {

        // create two Double objects 
        // with different values
        Double n1 = new Double(100.25);
        Double n2 = new Double(200.75);

        // compare them using equals()
        if (n1.equals(n2)) {
            System.out.println("n1 and n2 are equal.");
        } else {
            System.out.println("n1 and n2 are not equal.");
        }

        // create two Double objects 
        // with the same value
        Double n3 = new Double(99.99);
        Double n4 = new Double(99.99);

        // Compare them
        if (n3.equals(n4)) {
            System.out.println("n3 and n4 are equal.");
        } else {
            System.out.println("n3 and n4 are not equal.");
        }
    }
}

Output
n1 and n2 are not equal.
n3 and n4 are equal.


Example 2: In this example, we will see what happens when anything other than the object is passed as an argument.

Java
// Java program to demonstrate 
// Double.equals() with invalid arguments
import java.lang.Double;

public class Geeks {

    public static void main(String[] args) {

        Double v = new Double(55.55);

        // compare with a string
        if (v.equals("Education")) {
            System.out.println("Equal to string.");
        } else {
            System.out.println("Not equal to string.");
        }

        // compare with null
        if (v.equals(null)) {
            System.out.println("Equal to null.");
        } else {
            System.out.println("Not equal to null.");
        }
    }
}

Output
Not equal to string.
Not equal to null.


Example 3: In this example, we will see what happens when no argument is passed.

Java
// Java program to demonstrate incorrect 
// use of Double.equals() without an argument
import java.lang.Math;

public class Geeks {

    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:

error: method equals in class Double cannot be applied to given types;
if (obj1.equals())
^
required: Object
found: no arguments
reason: actual and formal argument lists differ in length

Note: The equals() method requires a single argument. If no argument is there, it will cause compile-time error.


Next Article
Practice Tags :

Similar Reads