Resource ownership and decimal money
Prerequisites: 01-ecosystem
What must be explicit at a boundary?
Goal & mental model verified
Garbage collection does not replace deterministic resource closure. AutoCloseable integrates with try-with-resources. BigDecimal models decimal arithmetic, with explicit precision/rounding decisions and scale-sensitive equals.
[S09] [S10]Worked example · illustrative, not executed synthesis
var premium = new java.math.BigDecimal("1250000.00");
var tax = premium.multiply(new java.math.BigDecimal("0.10"));
// Pair amounts with a currency and an explicit rounding policy.
try (var reader = java.nio.file.Files.newBufferedReader(path)) {
return reader.readLine();
}[S09] [S10]Engineering decision synthesis
Use decimal values or an explicitly specified minor-unit model for premiums. State currency, scale and rounding at business boundaries; neither approach chooses the policy for you.
[S09] [S10]Pitfall & diagnosis synthesis
BigDecimal constructed from a binary double can inherit representation artifacts. Leaking file/database resources can exhaust capacity even when heap usage appears healthy.
[S09] [S10]Improve & validate synthesis
Define resource ownership and close scope. Exercise rounding edges and failure paths; compare 1.0 and 1.00 with the intended numeric or representation semantics.
[S09] [S10]Check yourself: Does BigDecimal.equals ignore scale?
No. compareTo can test numeric equality; equals also distinguishes scale.