Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing String Representation of Number Literal with Suffices

 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan Kade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent. Thanks Ryan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic