1

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.)

Thomas Jung
  • 32,428
  • 9
  • 84
  • 114
soc
  • 27,983
  • 20
  • 111
  • 215

2 Answers2

3

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.

Atreys
  • 3,741
  • 1
  • 17
  • 27
  • I wonder if that's the right argument to be made. Not even the Java Language Specification for Java 7 has been updated and it is actually 2 weeks until release. – soc Jun 23 '11 at 20:02
  • 1
    @soc I argue that you shouldn't expect more than specified by the API. – Atreys Jun 23 '11 at 20:14
0

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.

Voo
  • 29,040
  • 11
  • 82
  • 156
  • a) How would it change the signature? b) Why would you consider them to be "java identifiers" instead of "numbers"? – soc Jun 23 '11 at 19:41
  • "signature" in the sense that the behavior of a function is obviously part of the definition of an interface. Declaring inputs valid that were earlier considered invalid obviously changes that. And that's a sacrilege for every stable API. About b) - go on the streets and ask people if they'd consider "1_2_3" a number or not - most people wouldn't. The language specification changed what it accepts as a valid number in the source code, nothing to do with what constitutes a number when parsing input. – Voo Jun 24 '11 at 15:53