Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › Variables, Data Types & Operators

When would you choose BigDecimal over double, and what is the one mistake people make constructing it?

Level II · JuniorVariables, Data Types & Operators
Short answer

Use BigDecimal whenever exact decimal arithmetic matters — money, billing, tax, anything user-facing where 0.1 + 0.2 must equal 0.3.

Explanation

The common mistake is new BigDecimal(0.1), which captures the inexact binary double; you must use new BigDecimal("0.1") or BigDecimal.valueOf(0.1) to get the exact decimal. Also set an explicit scale and RoundingMode on divide(), or it throws ArithmeticException on non-terminating results.

Common mistakes
  • Constructing BigDecimal from a double (new BigDecimal(0.1)) instead of a String or BigDecimal.valueOf.
  • Comparing BigDecimals with equals when scale differs (2.0 vs 2.00); use compareTo.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Variables, Data Types & Operators.

Practise free

More on Variables, Data Types & Operators