为什么数字类上的 valueOf/parse 方法拒绝 Java 7 中的有效数字?
下划线:
scala> java.lang.Float.valueOf("1_2_3.4_5_6")
java.lang.NumberFormatException: For input string: "1_2_3.4_5_6"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1242)
at java.lang.Float.valueOf(Float.java:416)
和二进制文字都不起作用:
scala> java.lang.Byte.valueOf("0b01010101")
java.lang.NumberFormatException: For input string: "0b01010101"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Byte.parseByte(Byte.java:148)
at java.lang.Byte.valueOf(Byte.java:204)
at java.lang.Byte.valueOf(Byte.java:230)
这些方法没有针对 Java 语言的变化进行更新的原因是什么?
(我实际上正在使用 OpenJDK 64 位服务器 VM、Java 1.7.0_136-icedtea。)
Neither underscores:
scala> java.lang.Float.valueOf("1_2_3.4_5_6")
java.lang.NumberFormatException: For input string: "1_2_3.4_5_6"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1242)
at java.lang.Float.valueOf(Float.java:416)
nor binary literals work:
scala> java.lang.Byte.valueOf("0b01010101")
java.lang.NumberFormatException: For input string: "0b01010101"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Byte.parseByte(Byte.java:148)
at java.lang.Byte.valueOf(Byte.java:204)
at java.lang.Byte.valueOf(Byte.java:230)
What's the reason those methods weren't updated for the changes in the Java language?
(I'm working with OpenJDK 64-Bit Server VM, Java 1.7.0_136-icedtea actually.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看着双。我希望其他数字类似:
valueOf 方法在 Java 7 中与 valueOf Java 6 中的方法。Java 7 的 API 明确规定不允许使用下划线。
数字文字中允许使用下划线,不可解析的字符串。这很可能是 @Voo 对 java 标识符的含义。
I looked at Double. I expect other Numbers are similar:
The syntax specified by the valueOf method in Java 7 did not change from the valueOf method in Java 6. Java 7's API specifically states that underscores aren't allowed.
Underscores will be allowed in numeric literals, not parsable strings. This was most likely @Voo's meaning by java identifiers.
最重要的是,你不能仅仅因为自己喜欢而更改公共 API 的签名;)
他们可以实现一些根据这些新更改解析浮点数的附加方法,但这些方法是用来解析数字的,不是 java 标识符,因此仅仅因为语言规范在这方面发生变化并不意味着浮点/整数的解析应该改变。
另请注意,到目前为止,Integer.valueOf 尚未接受“0x”,因此他们没有理由现在就开始使用它。
Well most of all because you can't just change the signature of a public API just because you feel like it ;)
They could implement some additional methods that parse floats according to these new changes, but then these methods are there to parse numbers, not java identifiers, so just because the language spec changed in that regard doesn't mean that the parsing of floating point/integer numbers should be changed.
Also note that Integer.valueOf hasn't accepted "0x" so far either, so there's no reason why they should start with it now.