Java Double.equals() Method (original) (raw)

Last Updated : 11 Jul, 2025

The Double.equals() in Java is a built-in function from the java.lang.Double class.** Thismethod 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)

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.