Definition
- Single comparison – if A > B the answer is A, else B; the else branch covers equality naturally.
- BigDecimal compareTo –
a.compareTo(b) > 0means A is strictly greater; compares numeric value, ignores scale. - BigDecimal equals trap –
equalschecks both value and scale, so12.0.equals(12.00)is false. - max method –
a.max(b)returns the larger BigDecimal directly; cleanest production code. - Null safety – validate inputs before comparing.
National Bank of India must apply the higher of two fee quotes on a transaction: one from the standard schedule and one from a promotional override. Rajesh's transaction has a standard fee of twelve rupees and an override of nine, so the bank compares and applies twelve.
Understanding the Concepts
What is the problem?
Read two numbers A and B, find and print the larger one. For 12 and 9, print 12. If they are equal, either is the answer.
How do you compare money with BigDecimal?
package com.bank.service;
import java.math.BigDecimal;
public class FeeSelector {
public static BigDecimal higherFee(BigDecimal standard, BigDecimal override) {
if (standard == null || override == null) {
throw new IllegalArgumentException("Fee must not be null");
}
return standard.max(override); // returns the larger
}
public static void main(String[] args) {
BigDecimal std = new BigDecimal("12.00");
BigDecimal promo = new BigDecimal("9.00");
System.out.println("Applied fee: " + higherFee(std, promo));
}
}
Applied fee: 12.00
Why use compareTo instead of equals?
BigDecimal equals checks both numeric value and scale. Two amounts that are numerically identical but stored with different scales are not equal by equals. compareTo compares only the numeric value.
BigDecimal a = new BigDecimal("12.0"); // scale 1
BigDecimal b = new BigDecimal("12.00"); // scale 2
System.out.println(a.equals(b)); // false (different scale!)
System.out.println(a.compareTo(b)); // 0 (same value)
What is the explicit comparison form?
When you need the decision branch (for a flowchart), use compareTo:
if (a.compareTo(b) > 0) {
System.out.println(a);
} else {
System.out.println(b); // also handles equality
}
What happens if you use == to compare BigDecimal objects?
The == operator checks reference identity, not numeric value. Two BigDecimal objects with the same value are different objects in memory, so == returns false. Always use compareTo for value comparison.
BigDecimal x = new BigDecimal("12.00");
BigDecimal y = new BigDecimal("12.00");
System.out.println(x == y); // false (different objects)
System.out.println(x.compareTo(y)); // 0 (same value)
What are edge cases?
- Both equal: either value is the maximum.
- One is zero: the other is the maximum (assuming non-negative fees).
- Different scales: compareTo handles them correctly.
- Negative values: compareTo works with negative BigDecimal amounts.
What are common mistakes?
// Wrong: using > on BigDecimal objects
if (a > b) { } // does not compile!
// Wrong: using equals for comparison
if (a.equals(b)) { } // scale-sensitive, not what you want
// Correct: compareTo for numeric comparison
if (a.compareTo(b) > 0) { }
// Cleanest: max method
BigDecimal larger = a.max(b);
How does this generalize to three or more values?
Chain max calls for a small fixed set: a.max(b).max(c). For a collection, use stream().reduce(BigDecimal::max). The reduce approach handles any number of values and returns an Optional to handle the empty case.
import java.math.BigDecimal;
import java.util.List;
List<BigDecimal> fees = List.of(
new BigDecimal("12.00"),
new BigDecimal("9.00"),
new BigDecimal("15.50")
);
BigDecimal highest = fees.stream()
.reduce(BigDecimal::max)
.orElse(BigDecimal.ZERO);
System.out.println("Highest fee: " + highest); // 15.50
How does this apply to a real banking scenario?
At National Bank of India, when Priya transfers funds between her accounts, the system compares the transfer amount against the daily limit and applies the smaller of the two. The logic is the same single comparison flipped to a min instead of a max.
BigDecimal requestedAmount = new BigDecimal("50000.00");
BigDecimal dailyLimit = new BigDecimal("25000.00");
BigDecimal allowed = requestedAmount.min(dailyLimit);
System.out.println("Allowed transfer: " + allowed); // 25000.00
What should a senior engineer know?
A senior engineer uses a.max(b) in production because it is concise and readable, but understands that the flowchart asks for the explicit decision. The compareTo approach is what you would show when explaining the algorithm step by step. In concurrent systems, both values should be read atomically to avoid comparing stale data.
What are common interview follow-up questions?
- What is the difference between BigDecimal equals and compareTo?
- How would you find the maximum of an array of BigDecimal values?
- What happens if you use == to compare two BigDecimal objects?
Visual Overview
┌─────────┐
│ Start │
└────┬────┘
▼
Read A, Read B
│
▼
┌─────────────────────┐
│ A.compareTo(B) > 0 │
└──┬───────────────┬──┘
│ Yes │ No
▼ ▼
Print A Print B
│ │
└──────┬────────┘
▼
┌─────┐
│ End │
└─────┘
Key Takeaway
Finding the larger of two values is a single comparison. If A is greater than B the answer is A, otherwise it is B, and the else branch naturally handles the equal case.
The important detail for money is how you compare. BigDecimal equals checks both value and scale, so amounts with different scales appear unequal even when they represent the same number. compareTo compares only the numeric value, which is what you want. In production, BigDecimal's max method does the comparison internally and returns the larger directly.
Relational operators like > do not work on objects at all, so compareTo is the only option for BigDecimal comparison. This is a common source of compilation errors for developers coming from primitive types.
In a banking project at National Bank of India, the fee-selection rule compared a standard fee and a promotional override, applying whichever was higher. The FeeSelector used compareTo and the else branch handled ties safely. Because compareTo ignores scale, the logic stayed correct even when quotes arrived with different decimal precision.
Remember one sentence: compare money with compareTo (not equals) because equals is scale-sensitive, and use a.max(b) for the cleanest production code.
---