Course contentsShow
← Java
Lesson 40 of 2,8702. Variables and Primitive TypesFree lesson

Floating-Point Literals and the f/F Suffix

Understand how to write floating-point literals and why the f suffix is required for float values.

Floating-Point Literals and the f/F Suffix

What you'll learn: How to correctly write decimal numbers in Java and why float values need a special suffix.

The Default: Everything is double

When you write a decimal number like 3.14 in Java, the compiler automatically treats it as a double type. This is Java's default for any floating-point literal. Think of it like writing a letter without specifying overnight delivery—regular mail (double) is assumed.

Why float Needs the f Suffix

If you want to store a value in a float variable, you must tell the compiler explicitly by adding an f or F suffix to the number:

float temperature = 98.6f;  // Correct
float price = 19.99F;        // Also correct (capital F works too)

Without the suffix, you'll get a compilation error because the compiler sees a double literal trying to fit into a float container:

float weight = 150.5;  // ERROR: double cannot fit into float

The double Type Needs Nothing

Since double is the default, you never need a suffix for double variables:

double distance = 42.195;  // No suffix needed

You can add a d or D suffix if you want to be explicit, but it's optional and rarely seen.

Key Takeaway: Java treats all decimal literals as double by default, so you must add the f or F suffix when assigning values to float variables to avoid compilation errors.