| Author |
Problem with the Wrapper Conversion
|
Faisal Fuad
Ranch Hand
Joined: Jul 05, 2011
Posts: 76
|
|
Guys...I was reading the Wapper Conversion rules and was trying to do the following, which is now giving me NumberFormatException. May be the basic part for Wrapper Class I am missing here to understand correctly. Can any one please help me to be on the right side?
OUTPUT:
Best wishes
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
|
You are trying to create an Integer with a double value. Similar to if you had tried
|
 |
Faisal Fuad
Ranch Hand
Joined: Jul 05, 2011
Posts: 76
|
|
But why that is not Widening the value for me? Isn't it an automatic process in Java? Or what i was trying to do is illegal in Java ! I know that Wrapper's second big function is convering stuff....so why can't i do that here?
Can you please show me a good example on converting a String to a Wrapper?
Thanks
|
 |
Muhammad Saifuddin
Ranch Hand
Joined: Dec 06, 2005
Posts: 1318
|
|
Yes, its illegal in Java, and you can check yourself.
see if you're able to compile above code?
|
Saifuddin..
[Linkedin] How To Ask Questions On JavaRanch My OpenSource
|
 |
Faisal Fuad
Ranch Hand
Joined: Jul 05, 2011
Posts: 76
|
|
|
Thanks both Dennis and Muhammad...yes...certainly i can't compile that above code....but here trying to under stand how particularly Wrapper can help me, though it's second best function is to conver.
|
 |
Faisal Fuad
Ranch Hand
Joined: Jul 05, 2011
Posts: 76
|
|
Ok...let me share this...the following is clear:
Is there any Good example for Wrapped String to Primitive? (In this case Integer)
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Faisal:
Strings aren't wrapped. If you look at the JavaDoc for Integer.valueOf(String s), you'll see that it's quite clear that if the String doesn't represent an integer value, you'll see the NumberFormatException you got. If you want to convert a String representing a floating point number, you'll have to add an intermediate step.
John.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Faisal Fuad wrote:But why that is not Widening the value for me?. . .
I know it’s late, but that is not widening. It’s narrowing. More details in the Java Language Specification.
|
 |
 |
|
|
subject: Problem with the Wrapper Conversion
|
|
|