| Author |
Parsing String Representation of Number Literal with Suffices
|
Joshua Smith
Ranch Hand
Joined: Aug 22, 2005
Posts: 192
|
|
Here are examples of parsing String literal representations of numeric literals with suffixes. Why does parsing a float from 1f and 1d work, but parsing a float from 1L fail and parsing bytes from 1L, 1f and 1d all fail? Thanks, Josh
|
Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
|
 |
Ryan Kade
Ranch Hand
Joined: Aug 16, 2005
Posts: 69
|
|
The parse functions of the wrapper classes require that the input string be parseable text that is equivalent to the corresponding primitive. You couldn't actually assign a double, long, or float to a byte, so why should Byte let you do it via a String? [ August 24, 2005: Message edited by: Ryan Kade ]
|
 |
Joshua Smith
Ranch Hand
Joined: Aug 22, 2005
Posts: 192
|
|
I guess I was expecting a different error message since the String is technically parsable as a number. Still you're right. Those assignments can't be made without an explicit cast. Josh
|
 |
Ryan Kade
Ranch Hand
Joined: Aug 16, 2005
Posts: 69
|
|
True, the strings are technically parseable as a number, but that's not what the contract specifies. The SDK docs for Integer.parseInt say an exception is thrown if "The value represented by the string is not a value of type int." You'll find similar language in each of the wrapper class docs.
|
 |
Joshua Smith
Ranch Hand
Joined: Aug 22, 2005
Posts: 192
|
|
|
Excellent. Thanks Ryan.
|
 |
 |
|
|
subject: Parsing String Representation of Number Literal with Suffices
|
|
|