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

Floating-Point Literals and the F and D Suffixes

Learn how to write float literals with F/f and double literals with D/d suffixes.

Floating-Point Literals and the F and D Suffixes

You'll learn how to explicitly mark floating-point numbers as float or double using suffix letters.

The Default Assumption

When you write a decimal number like 3.14 in your Java code, the compiler automatically treats it as a double. This is Java's default for all floating-point literals. But what if you want to store that value in a float variable? You need to tell the compiler explicitly.

Using the F/f Suffix for float

To create a float literal, append the letter F or f to your number:

float pi = 3.14f;
float temperature = 98.6F;

Without the f suffix, the compiler would complain because you'd be trying to fit a double (64 bits) into a float variable (32 bits).

Using the D/d Suffix for double

Although double is already the default, you can explicitly mark a literal with D or d for clarity:

double gravity = 9.81d;
double distance = 42.195D;

Most programmers omit the D since it's redundant, but it's valid syntax and can sometimes make your intent clearer when reading code.

Why This Matters

Think of suffixes as labels on containers: the F says "this is a small container (float)," while no suffix or D says "this is a large container (double)." Using the wrong label causes the compiler to reject your code—it won't automatically shrink a double into a float without your permission.

Key Takeaway: Use the F or f suffix to create float literals; double literals need no suffix (but accept D/d for clarity).