File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Classes

 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a questioni from K&B



valueOf() -> String to Wrapper
XXXValue() -> Wrapper to Primitive
parseXXX() -> String to Primitive

I thought the answer would be

line 5 -> It return a wrapper as valueOf() methods are supposed to return.
line 6 -> returns a Primitive
line 9 -> takes a String and returns a primitive.

Isn't this what we learned about the methods.
THen how come my answer are exactly opposite to the answer.

Or have i misunderstood the question?
 
subhasish nag
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nabila,

Here Autoboxing works in java 5
in line 4 ,here what actually happen valueOf() returns Wrapper(Long) and unwrap it to premitive (long).
in line 7 and 9 both methods retunrns premitive and wrap it to Wrapper.
If you use java 4 autoboxing will not work and it will give compiler error.

subhasish
 
Sunny Mattas
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B][/B]

Hi,
I do have shown return type of each method.Now we have
At line 4 valueOf() returns Long object.Long is return type of valueOf().Therefore it will give error as autoboxing doesnot applies to java 4

Method longValue at line 7 returns primitive long, which could not be autoboxed to wrapper object Long.Thus errors occurs.

At 8 parseLong will also return a primitive type and you know there is no autoboxing in java 4.here is a error again.

Regards
Sunny
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys...
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To memorize which one returns the primitive or the wrapper think like this:
If the method has the name of the primitive, it returns the primitive, if not, returns the Wrapper.

So, valueOf() returns Wrapper.
And parseXXX and xxxValue() returns the primitive.
[ July 23, 2008: Message edited by: Raphael Rabadan ]
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey that's a great way to learn...

Thanks for sharing...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic