什么是“0”的含义?作为前缀在浮点文字? [英] What's the meaning of "0" as prefix in a floating point literal?

查看:264
本文介绍了什么是“0”的含义?作为前缀在浮点文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用0(零)作为整数字面上的前缀将其基数改为八进制。这就是为什么

  System.out.println(010); 

会打印 8 。但是使用F作为后缀

  System.out.println(010F); 

会使它浮动丢失八进制数(回到十进制数)并打印 10.0

010F 10F 之间是否有区别?当使用浮点数时,有没有0前缀的意义?

解决方案

从Java语言规范,在浮点文字


FloatingPointLiteral:


  • DecimalFloatingPointLiteral












  • $
  • 数字。 [Digits] [ExponentPart] [FloatTypeSuffix]

  • 。 Digits [ExponentPart] [FloatTypeSuffix]

  • 数字ExponentPart [FloatTypeSuffix]

  • 数字[ExponentPart] FloatTypeSuffix
>

其中数字


位数:




  • 数字

  • 数字 [DigitsAndUnderscores] Digit








  • DigitAndUnderscores:


    • DigitOrUnderscore {DigitOrUnderscore}



    DigitOrUnderscore:


    • 数字
    • _



    • 下划线: / ul>
  • 对于浮点文字,您可以有任意数量的前导 0



    我无法在JLS中找到任何解释为何允许这样做的内容,但是我可以想象它简化了解析。


    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.

    这篇关于什么是“0”的含义?作为前缀在浮点文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆