• 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

primitive conversion

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic