• 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

Wrapper Classes

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys...
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey that's a great way to learn...

Thanks for sharing...
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic