Java - Double Class equals() Method

Double class equals() method: Here, we are going to learn about the equals() method of Double class with its syntax and example. By Preeti Jain Last updated : March 23, 2024

Double class equals() method

  • equals() method is available in Double class of java.lang package.
  • equals() method is used to check equality or inequality of this Object against the given Object or in other words we can say this method is used to compare two objects.
  • equals() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • equals() method does not throw an exception at the time of comparing two Objects.

Syntax

public boolean equals(Object ob);

Parameters

  • Object ob – represents the Object to compare with.

Return Value

The return type of this method is boolean, it returns a Boolean value. it returns a Boolean value. If both objects are equal it returns "true", else it returns "false".

Example

// Java program to demonstrate the example 
// of boolean equals(Object ob) method of Double class

public class DoubleToLongBitsOfDoubleClass {
    public static void main(String[] args) {
        // Variables initialization
        Double ob1 = new Double(10.0);
        Double ob2 = new Double(20.0);

        // Display value1,value2 values
        System.out.println("ob1: " + ob1);
        System.out.println("ob2: " + ob2);

        // It compare two objects of Double type
        // by calling ob1.equals(ob2)
        boolean compare = ob1.equals(ob2);

        // Display result values
        System.out.println("ob1.equals(ob2): " + compare);

        if (compare == true)
            System.out.println("Both objects are equal");
        else
            System.out.println("Both objects are not equal");
    }
}

Output

ob1: 10.0
ob2: 20.0
ob1.equals(ob2): false
Both objects are not equal

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.