Comparing Numbers in Java

A Double is NEVER equals to an Integer. Moreover, a double is not the same as a Double.

Java has primitive types and reference types. The truly numeric types in Java do not extend from Number, because they're primitives.

You may want to consider a system where you're not mixing types, because that usually will cause a lot of trouble with implicit/explicit conversions that may/may not lose information, etc.

Related questions

On int vs Integer:

  • What is the difference between an int and an Integer in Java/C#?
  • Is Java fully object-oriented?

On Number comparison:

  • Why doesn't java.lang.Number implement Comparable?
  • Comparing the values of two generic Numbers

See also

  • Java Language Guide/Autoboxing
  • JLS 4.2 4.2 Primitive Types and Values

    The numeric types are the integral types and the floating-point types. The integral types are byte, short, int, and long and char. The floating-point types are float and double.


On mixed-type computation

Mixed-type computation is the subject of at least 4 puzzles in Java Puzzlers.

Here are various excerpts:

it is generally best to avoid mixed-type computations [...] because they are inherently confusing [...] Nowhere is this more apparent than in conditional expressions. Mixed-type comparisons are always confusing because the system is forced to promote one operand to match the type of the other. The conversion is invisible and may not yield the results that you expect

Prescription: Avoid computations that mix integral and floating-point types. Prefer integral arithmetic to floating-point.


To compare two Numbers in Java you can use the compareTo from BigDecimal. BigDecimal can hold everything from short until double or BigInteger, so it's the perfect class for this.

So you can try to write something like this:

public int compareTo(Number n1, Number n2) {
    // ignoring null handling
    BigDecimal b1 = BigDecimal.valueOf(n1.doubleValue());
    BigDecimal b2 = BigDecimal.valueOf(n2.doubleValue());
    return b1.compareTo(b2);
}

This is surely not the best approach regarding to performance. The following tests worked so far, at least with JDK7:

assertTrue(compareTo(new Integer(1), new Integer(2)) == -1);
assertTrue(compareTo(new Integer(1), new Double(2.0)) == -1);
assertTrue(compareTo(new Integer(1), new Double(Double.MAX_VALUE)) == -1);
assertTrue(compareTo(new Integer(1), new Double(Double.MIN_VALUE)) == 1);
assertTrue(compareTo(new Integer(1), new Double(1.000001)) == -1);
assertTrue(compareTo(new Integer(1), new Double(1.000)) == 0);
assertTrue(compareTo(new Integer(1), new Double(0.25*4)) == 0);
assertTrue(compareTo(new Integer(1), new AtomicLong(1)) == 0);