Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between double and Double in comparison

Tags:

I know that Double is a a wrapper class, and it wraps double number. Today, I have seen another main difference :

double a = 1.0; double b = 1.0; Double c = 1.0; Double d = 1.0; System.out.println(a == b);  // true System.out.println(c == d);  // false 

So strange with me !!!

So, if we use Double, each time, we must do something like this :

private static final double delta = 0.0001; System.out.println(Math.abs(c-d) < delta);  

I cannot explain why Double make directly comparison wrong. Please explain for me.

like image 589
hqt Avatar asked Sep 01 '12 09:09

hqt


People also ask

What is difference between double and double?

Double is an object and double is a primitive data type. See this answer for more details. The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.

How do you compare two double values?

The compare() method of Double Class is a built-in method in Java that compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call.

Can you use == to compare doubles?

Using the == Operator As a result, we can't have an exact representation of most double values in our computers. They must be rounded to be saved. In that case, comparing both values with the == operator would produce a wrong result.

What is difference between == and equals in Java?

In java both == and equals() method is used to check the equality of two variables or objects. == is a relational operator which checks if the values of two operands are equal or not, if yes then condition becomes true. equals() is a method available in Object class and is used to compare objects for equality.


2 Answers

c and d are technically two different objects and == operator compares only references.

c.equals(d) 

is better as it compares values, not references. But still not ideal. Comparing floating-point values directly should always take some error (epsilon) into account (Math.abs(c - d) < epsilon).

Note that:

Integer c = 1; Integer d = 1; 

here comparison would yield true, but that's more complicated (Integer internal caching, described in JavaDoc of Integer.valueOf()):

This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

Why valueOf()? Because this method is implicitly used to implement autoboxing:

Integer c = Integer.valueOf(1); Integer d = Integer.valueOf(1); 

See also

  • Weird Integer boxing in Java
  • How to properly compare two Integers in Java?
like image 190
Tomasz Nurkiewicz Avatar answered Sep 23 '22 21:09

Tomasz Nurkiewicz


When applied to expressions of a class type, == will always perform a reference comparison (JLS section 15.21.3). So this line:

System.out.println(c == d);  

is checking whether c and d refer to the same objects. Auto-boxing in Java always (I believe) creates a new object for float and double (the situation is more complicated for integral types1). Therefore c and d refer to different objects, and so it prints false.

If you want to compare objects for equality, you need to call equals explicitly:

System.out.println(c.equals(d)); 

With double, it's using numeric equality instead - as specified in section 15.21.1. Hence the difference in behaviour.


1 For integral autoboxing, "small" values are cached - so autoboxing 5 (say) will return the same reference every time. The definition of "small" is implementation-specific, but it's guaranteed within the range -128 to 127. See the bottom of section 5.1.7 for details.

like image 34
Jon Skeet Avatar answered Sep 24 '22 21:09

Jon Skeet