why f is placed after float values?

Seeing as there are only so many ways to represent a number in your program, the designers of Java had to cherry pick and assign each form to the most common use case. For those forms selected as default, the suffix that denotes the exact type is optional.

  • For Integer literals (int, long), the default is int. For obvious reasons.
  • For Floating point literals (float, double) the default is double. Because using double potentially allows safer arithmetic on the stored values.

So, when you type 12 in your program, thats an int literal, as opposed to 12L, which is a long. And when you type 12.3, thats a double literal, as opposed to 12.3F, which is a float.

So where is this relevant? Primarily in handling downcasts, or narrowing conversions. Whenever you downcast a long to an int, or a double to a float, the possibility for data loss exists. So, the compiler will force you to indicate that you really want to perform the narrowing conversion, by signaling a compile error for something like this:

float f = 12.3;

Because 12.3 represents a double, you have to explicitly cast it to a float (basically signing off on the narrowing conversion). Otherwise, you could indicate that the number is really a float, by using the correct suffix;

float f = 12.3f;

So too summarize, having to specify a suffix for longs and floats is a compromise the language designers chose, in order to balance the need to specify what exactly a number is, with the flexibility of converting numbers from one storage type to another.


By default 12.3 is double literal. To tell compiler to treat it as float explicitly -> use f or F.

See tutorial page on the primitive types.