wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Parsing String Representation of Number Literal with Suffices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Parsing String Representation of Number Literal with Suffices" Watch "Parsing String Representation of Number Literal with Suffices" New topic
Author

Parsing String Representation of Number Literal with Suffices

Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
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: 193
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: 193
Excellent. Thanks Ryan.
 
 
subject: Parsing String Representation of Number Literal with Suffices
 
Similar Threads
wrapper class doubt
Question on float variables
comparison methods
How to read "0000 0001" from keyboard?
Leading + in constructor