Using "0" (zero) as a prefix in an integer literal changes its base to octal. This is why
System.out.println(010);
will print 8. But using "F" as a suffix
System.out.println(010F);
will make it float losing octal base (going back to decimal) and will print 10.0.
Is there any difference between 010F and 10F? Has the "0" prefix any kind of meaning when working with floats?
From the Java Language Specification, on Floating Point literals
FloatingPointLiteral:
- DecimalFloatingPointLiteral
- HexadecimalFloatingPointLiteral
DecimalFloatingPointLiteral:
- Digits . [Digits] [ExponentPart] [FloatTypeSuffix]
- . Digits [ExponentPart] [FloatTypeSuffix]
- Digits ExponentPart [FloatTypeSuffix]
- Digits [ExponentPart] FloatTypeSuffix
where Digits
Digits:
- Digit
- Digit [DigitsAndUnderscores] Digit
Digit:
- 0
- NonZeroDigit
DigitsAndUnderscores:
- DigitOrUnderscore {DigitOrUnderscore}
DigitOrUnderscore:
- Digit
- _
Underscores:
- _ {_}
You can have any number of leading 0 for floating point literals.
I cannot find anything in the JLS that explains why this is allowed, but I can imagine it simplifies parsing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With