Build a comparator:
Comparator<BigDecimal> c = Comparator.nullsFirst(Comparator.naturalOrder());
(Or nullsLast, it doesn't matter if you're only ever comparing to zero).
Then:
if (c.compare(first, second) != 0) {
// ...
}
Answer from Andy Turner on Stack OverflowBuild a comparator:
Comparator<BigDecimal> c = Comparator.nullsFirst(Comparator.naturalOrder());
(Or nullsLast, it doesn't matter if you're only ever comparing to zero).
Then:
if (c.compare(first, second) != 0) {
// ...
}
Try using
CompareToBuilder
class provided by
org.apache.commons.lang3
as it make null safe comparison. Sample code is:
public static <T, U> int nullSafeComparison(T t, U u) {
return new CompareToBuilder().append(t, u).toComparison();
}
public static void main(String[] args) {
BigDecimal zero = BigDecimal.ZERO;
BigDecimal zeroPzero = new BigDecimal("0.0");
System.out.println( zero + " " + zeroPzero);
System.out.println(nullSafeComparison(zero, zeroPzero));
}
If both numbers are same it will return 0, if 1st is greater than 2nd the result will be 1 and -1 if 1st number is less than 2nd.
If your main goal is validating BigDecimal dataType for nulls, then just make a comparison;
yourBigDecimal != null.
The above statement is enough for comparison and checking.
Your || should be an && - what's happening is that when you pass in a null value it's evaluating to false in the first two conditions, but then it's proceeding on to the third and fourth conditions and resulting in an exception. If you change the || to an && then short circuit evaluation will prevent the third and fourth conditions from evaluating.
Be certain to use a && and not a & - the former uses short-circuit evaluation, but the latter would force the third and fourth conditions to evaluate and you'll get a null pointer exception again. condition1 && condition2 says "return false if condition1 is false, else evaluate condition2" - if condition1 is false then condition2 is never evaluated. In contrast, condition1 & condition2 will always evaluate both conditions. The only reason to use & is if the conditions have side-effects. Likewise, condition1 || condition2 will not evaluate condition2 if condition1 is true, but | will always evaluate both conditions.
Use compareTo(BigDecimal.ZERO) instead of equals():
if (price.compareTo(BigDecimal.ZERO) == 0) // see below
Comparing with the BigDecimal constant BigDecimal.ZERO avoids having to construct a new BigDecimal(0) every execution.
FYI, BigDecimal also has constants BigDecimal.ONE and BigDecimal.TEN for your convenience.
Note!
The reason you can't use BigDecimal#equals() is that it takes scale into consideration:
new BigDecimal("0").equals(BigDecimal.ZERO) // true
new BigDecimal("0.00").equals(BigDecimal.ZERO) // false!
so it's unsuitable for a purely numeric comparison. However, BigDecimal.compareTo() doesn't consider scale when comparing:
new BigDecimal("0").compareTo(BigDecimal.ZERO) == 0 // true
new BigDecimal("0.00").compareTo(BigDecimal.ZERO) == 0 // true
Alternatively, signum() can be used:
if (price.signum() == 0) {
return true;
}