File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper Classes" Watch "Wrapper Classes" New topic

Wrapper Classes

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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?

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
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.


Thanks,<br />Subhasish
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45

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 is a error again.


Sunny Mattas
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks guys...
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
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 ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hey that's a great way to learn...

Thanks for sharing...
I agree. Here's the link:
subject: Wrapper Classes
It's not a secret anymore!