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

primitive conversion

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic