Why do the valueOf/parse methods on the number classes reject valid numbers in Java 7?
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.Fl开发者_JS百科oat.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.)
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.
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.
精彩评论