Java: Why do you need to specify an ‘f’ in a float literal? [duplicate]

Because otherwise it defaults to double, which is a more commonly used floating point type than float.

From the Java Language Specification, section 3.10.2:

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d (ยง4.2.3).

(Personally I’d rather there were no default, to make it clear in all cases, but that’s a different matter.)

Leave a Comment