File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Problem with the Wrapper Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with the Wrapper Conversion" Watch "Problem with the Wrapper Conversion" New topic

Problem with the Wrapper Conversion

Faisal Fuad
Ranch Hand

Joined: Jul 05, 2011
Posts: 83

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?


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: 83

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 why can't i do that here?

Can you please show me a good example on converting a String to a Wrapper?

Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1323

Yes, its illegal in Java, and you can check yourself.

see if you're able to compile above code?

[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Faisal Fuad
Ranch Hand

Joined: Jul 05, 2011
Posts: 83

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: 83

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

Joined: Mar 09, 2009
Posts: 600

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.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
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.
I agree. Here's the link:
subject: Problem with the Wrapper Conversion
It's not a secret anymore!