Quantcast
Channel: Difference between double and Double in comparison - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Answer by Tomasz Nurkiewicz for Difference between double and Double in comparison

$
0
0

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


Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>