• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing String Representation of Number Literal with Suffices

 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent. Thanks Ryan.
reply
    Bookmark Topic Watch Topic
  • New Topic