Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equals operator for zeros (BigDecimal / Double) in Java

A few interesting observations w.r.t equals operator on 0 and 0.0

  1. new Double(0.0).equals(0) returns false, while new Double(0.0).equals(0.0) returns true.

  2. BigDecimal.ZERO.equals(BigDecimal.valueOf(0.0)) returns false, while BigDecimal.ZERO.equals(BigDecimal.valueOf(0)) returns true.

Looks like the string comparison is being done in both the cases. Could anyone throw some light on this.

Thanks.

like image 813
Manish Mulani Avatar asked Apr 03 '12 11:04

Manish Mulani


People also ask

Can we use equals operator for Double in Java?

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.

How to check BigDecimal value is zero in Java?

Using the compareTo Method Two BigDecimal objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. Therefore, we can check BigDecimal. ZERO. compareTo(givenBdNumber) == 0 to decide if givenBdNumber has the value zero.

How to compare BigDecimal and Double in Java?

Convert the double to a BigDecimal, using rounding, and then use compareTo(). Note that equals() takes into account decimal positions, so compareTo() is safer. You may need to round the BigDecimal as well.

How to check if two BigDecimals are equal?

math. BigDecimal. equals() method checks for equality of a BigDecimal value with the object passed. This method considers two BigDecimal objects equal if only if they are equal in value and scale.


2 Answers

BigDecimal 'equals' compares the value and the scale. If you only want to compare values (0 == 0.0) you should use compareTo:

BigDecimal.ZERO.compareTo(BigDecimal.valueOf(0.0)) == 0 //true BigDecimal.ZERO.compareTo(BigDecimal.valueOf(0)) == 0 //true 

See the javadoc.

As for the Double comparison, as explained by other answers, you are comparing a Double with an Integer in new Double(0.0).equals(0), which returns false because the objects have different types. For reference, the code for the equals method in JDK 7 is:

public boolean equals(Object obj) {     return (obj instanceof Double)            && (doubleToLongBits(((Double)obj).value) ==                   doubleToLongBits(value)); } 

In your case, (obj instanceof Double) is false.

like image 171
assylias Avatar answered Oct 12 '22 11:10

assylias


  1. The 0 in your first expression is interpreted as an int, which may be autoboxed into an Integer, but not to a Double. So the type of the two is different, hence they are not equal. OTOH 0.0 is a double, which is autoboxed into a Double, so the two operands are deemed equal.

  2. BigDecimals also contain a scale (i.e. number of digits to the right of the decimal separator dot). BigDecimal.ZERO has the value of "0", so its scale is 0. Hence it is not equal to "0.0", whose scale is 1.
    If you want to compare values, use BigDecimal.compareTo:

    BigDecimal.ZERO.compareTo(BigDecimal.valueOf(0.0)) == 0 BigDecimal.ZERO.compareTo(BigDecimal.valueOf(0)) == 0 
like image 30
Péter Török Avatar answered Oct 12 '22 11:10

Péter Török