Skip to content

Floating‑point types (float, double)

Java provides two types for numbers with fractional parts.

TypeSizePrecisionDefault
float4 bytes6‑7 significant digits0.0f
double8 bytes15 significant digits0.0d

float literals must end with f or F:

float pi = 3.14159f;

double is the default for decimal literals:

double e = 2.718281828459045;

You can also use scientific notation:

double avogadro = 6.022e23; // 6.022 × 10²³
float planck = 6.626e-34f; // 6.626 × 10⁻³⁴

Special values:

  • Float.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY
  • Float.NaN, Double.NaN (Not‑a‑Number)

Be careful with floating‑point comparisons:

double result = 0.1 + 0.2;
System.out.println(result == 0.3); // false!

Due to binary representation, use a tolerance for equality:

double epsilon = 1e-10;
if (Math.abs(result - 0.3) < epsilon) {
System.out.println("Approximately equal");
}

When to use:

  • float – memory‑sensitive applications (e.g., 3D graphics, arrays with millions of elements)
  • double – default for most calculations (better precision)