java.lang.Double equals(Object obj)

Syntax:

public boolean equals(Object obj)

Java Code Example :

This java example source code demonstrates the use of equals(Object obj) method of Double class.

Some takeaways on the equals(Object obj)  method:

  • Compares this object against the specified object. The result is true if and only if the argument is not null and is a Double object that represents a double that has the same value as the double represented by this object. For this purpose, two double values are considered to be the same if and only if the method doubleToLongBits(double) returns the identical long value when applied to each.
  • Equality would be true sometimes if the following is true: d1.doubleValue() == d2.doubleValue()
  • overrides equals in class Object
package com.javatutorialhq.java.examples;

import static java.lang.System.out;

/*
 * This example source code demonstrates the use of  
 * equals(Object obj) method of Double class
 */

public class DoubleEqualsObject {

	public static void main(String[] args) {
		
		

		Double val1 = 321.25;
		Double val2 = 321.25;
		Double val3 = 321.2503;
		Object obj = new Double(321.25);
		
		/* 
		 * test equality between val1
		 * and val2 which is having the same Double value
		 */		
		out.println("val1 is equals to val2? "+val1.equals(val2));
		
		/*
		 * Test equality between val2 and val3
		 * which does not have equal value
		 *  
		 */
		out.println("val2 is equals to val3? "+val2.equals(val3));
		
		/*
		 * Test equality of object obj and val1
		 */
		out.println("obj is equals to val1? "+obj.equals(val1));
		
	}
}

Sample Output :

Running the above example source code will give the following output

val1 is equals to val2? true
val2 is equals to val3? false
obj is equals to val1? true

Suggested Reading List :

References :