Wednesday, July 3, 2024
HomeLanguagesJavaDouble.equals() Method in Java with Examples

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:




// 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");
    }
}


Output:

Object1 & Object2: Not equal
Object1 & Object2: Equal

Program 2: When no argument is passed.




// 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");
    }
}


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() 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");
    }
}


Output:

Object1 & Object2: Not Equal

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments