wood burning stoves 2.0*
The moose likes Associate Certification (OCAJP 7) and the fly likes Wrapper class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7)
Bookmark "Wrapper class question" Watch "Wrapper class question" New topic
Author

Wrapper class question

Luca Cazzaniga
Greenhorn

Joined: Feb 16, 2013
Posts: 12
Hi all
what happens when the code creates a new Wrapper obj assigning a primitive value as follows

Short s=2;

It compiles.. but I don't get why.. shouldn't the compiler autobox the primitive value with an implicite call to Integer.valueOf(2)?
I presumed so becouse there is only a method Short.valueOf(short) and calling it with a integer argument yields a compilation issue:

error: no suitable method found for valueOf(int)
Short s=Short.valueOf(2);

so I thought the code yelds to a wrong assignment as
Short s=Integer.valueOf(2);

I tried to use javap to decompile but I can't decode the output..

Could you confirm the wrapper class is matter of the oca test?
I found some questions about that in the Deshmukh's oca 1z0-803 book so I would like to dig into the subject..

Any helps will be really appreciate.


Best regards
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5226
    
  12

I think the Java compiler is a real brainiac

Let's take this little program:


When I decompile the compiled class file, I get this java code:


So the java compiler does a whole lot behind the scenes!


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Luca Cazzaniga
Greenhorn

Joined: Feb 16, 2013
Posts: 12

Hi Roel, thanks for your time and enlightments,

can I ask you what do you use to decompile? Does the decompiler's output provide a straight way to show all the work behind the scene and understand the building steps?


Best regards

Luca
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5226
    
  12

Hi Luca,

I have always used the JAva Decompiler (JAD). It isn't maintained anymore, but it still does the job really good. It has already given me useful information many times (e.g. what happens when you use an enhanced-for loop).

Have fun!
Kind regards,
Roel
Luca Cazzaniga
Greenhorn

Joined: Feb 16, 2013
Posts: 12
Jad was the missing brick for the building tests. Surely I'll improve my training..

Thanks again

Luca

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Wrapper class question