The Wayback Machine - https://web.archive.org/web/20240708032612/https://www.geeksforgeeks.org/double-equals-method-in-java-with-examples/
Open In App

Double.equals() Method in Java with Examples

Last Updated : 19 Jun, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

Java Guava | Doubles.indexOf(double[] array, double target) method with Examples
Doubles.indexOf(double[] array, double target) method of Guava's Doubles Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(double[] array, double target)
3 min read
Java Guava | Doubles.indexOf(double[] array, double[] target) method with Examples
Doubles.indexOf(double[] array, double[] target) method of Guava's Doubles Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(double[] array, double
3 min read
Java.util.Arrays.equals() in Java with Examples
Today we are going to discuss the simplest way to check whether two arrays are equal or not. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two arra
4 min read
Java Long equals() method with Examples
The java.lang.Long.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 Long object that contains the same long value as this object. It returns false if both the objects are not same. In all other cases, compareTo method should be preferred. S
3 min read
Character.equals() method in Java with examples
The java.lang.Character.equals() is a function in Java which compares this object against the specified object. If the argument is not null then the result is true and is a Character object that represents the same char value as this object. Syntax: public boolean equals(Object obj) Parameters: The function accepts a single parameter obj which spec
2 min read
Java 8 Clock equals() Method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.The equals() method of java.time.Clock class checks if two Clock objects are equal or not. If clocks are equal then it returns true, else it returns false. This equals method of clock class overrides this Object.equals(java.lang
3 min read
FloatBuffer equals() method in Java with Examples
The equals() method of java.nio.FloatBuffer Class is used to check whether or not the given buffer is equal to another object. Two float buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, considered independently of their starting position
4 min read
ShortBuffer equals() method in Java with Examples
The equals() method of java.nio.ShortBuffer Class is used to check whether or not the given buffer is equal to another object. Two short buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, considered independently of their starting position
3 min read
DoubleBuffer equals() method in Java with Examples
The equals() method of java.nio.DoubleBuffer Class is used to check whether or not the given buffer is equal to another object. Two double buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, considered independently of their starting positi
3 min read
ByteBuffer equals() method in Java with Examples
The equals() method of java.nio.ByteBuffer class is used to check whether or not the given buffer is equal to another object. Two byte buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, considered independently of their starting positions,
3 min read
Practice Tags :
three90RightbarBannerImg