Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# primitive conversion

Veena Pointi
Ranch Hand
Posts: 442
In the method
public static Integer valueOf(String s) throws NumberFormatException
Throws:
NumberFormatException - if the string cannot be parsed as an integer.
what is meant by parsed?Does it mean if it can not be convertible to int?
Veena

Dave Johnson
Ranch Hand
Posts: 111
Parse literally means to analyze. In this case this method takes a String as an arguement which can used to create a primitive type. i.e. int etc.

Mani Ram
Ranch Hand
Posts: 1140
"Represent" will be a more proper word than "convertible"
Say when the String passed is "100", there won't be any trouble because it can be represented as an integer (100).
But if the input String is "abc", which cannot be represented as an integer then the exception is thrown.

Barkat Mardhani
Ranch Hand
Posts: 787
Again, at run time if string contains an integer value which is beyond int limits, exception will be thrown.

Veena Pointi
Ranch Hand
Posts: 442
if it is meaning representing an int,if we give 0x16(which is hexadecimal integer representation) as an argument it gives compiler error
Veena

Steve Lovelace
Ranch Hand
Posts: 125
If you do this:
Float f = new Float(0x16);
it works just fine. That's the same as:
Float f = new Float(22);
0x16 and 22 are integer literals representing the same number. However if you try this:
Float f = new Float("0x16");
you will get a NumberFormatException. The string "0x16" is not an integer literal, it is a string representation of an integer literal. The jvm routine which converts strings to floating point values is happy to take "22" or "22f" or "22e0", but for whatever reason doesn't seem to like representations of literals in radices other than 10.

Edwin Keeton
Ranch Hand
Posts: 214
Actually the string representation of 0x16 is "16".
works just fine as does where in both cases the second parameter is the radix.
But, fortunately, valueOf() and parseXXX() are not overloaded in Float and Double. (Think of how maddening it would be to convert the decimal part of the number to a different radix.)
The "String" constructors of all the wrapper classes interpret the parameter as radix 10.

Veena Pointi
Ranch Hand
Posts: 442
Originally posted by Edwin Keeton:
Actually the string representation of 0x16 is "16".
works just fine as does where in both cases the second parameter is the radix.
But, fortunately, valueOf() and parseXXX() are not overloaded in Float and Double. (Think of how maddening it would be to convert the decimal part of the number to a different radix.)
The "String" constructors of all the wrapper classes interpret the parameter as radix 10.

Thanks Edwin.