Part 5 · Fundamentals

Resource ownership and decimal money

Prerequisites: 01-ecosystem

What must be explicit at a boundary?

Owner scope: opens resource; Use resource: success or exception; close(): scope exits; Money policy: currency + rounding. Connections: Owner scope to Use resource (try-with-resources); Use resource to close() (always on exit)
Top: deterministic resource lifetime. Bottom: decimal policy is a separate domain decision. [S09] [S10]

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]
Keep this: Memory, resources and money each need their own ownership rules.
Check yourself: Does BigDecimal.equals ignore scale?

No. compareTo can test numeric equality; equals also distinguishes scale.

Sources & further reading

  1. [S09] AutoCloseable

    Oracle / OpenJDK · documentation · accessed 2026-10-09 · Java SE 25

    Supports: Resource closure with try-with-resources

    Read the linked section to validate the mechanism and its version-specific constraints.

  2. [S10] BigDecimal

    Oracle / OpenJDK · documentation · accessed 2026-10-09 · Java SE 25

    Supports: Decimal arithmetic; Rounding; Scale-sensitive equality

    Read the linked section to validate the mechanism and its version-specific constraints.