Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of "0" as prefix in a floating point literal?

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?

like image 330
Luigi Cortese Avatar asked May 28 '15 14:05

Luigi Cortese


1 Answers

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.

like image 176
Sotirios Delimanolis Avatar answered Nov 15 '22 00:11

Sotirios Delimanolis