wood burning stoves*
The moose likes Beginning Java and the fly likes Number Wrapper classes - autoboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Number Wrapper classes - autoboxing" Watch "Number Wrapper classes - autoboxing" New topic
Author

Number Wrapper classes - autoboxing

Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62

Does the constructor of Integer class get called, when I execute above line of code?


SCJP6
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

No. The method Integer.valueOf(int i) is called.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
My actual confusion is why doesn't below code compile?


where as this compiles?


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Auto-boxing only works on literals and variables of the same type. In your second example, you use an explicit method call and the short is widened to an int.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Rob Prime wrote:Auto-boxing only works on literals and variables of the same type.


Thank You, I will keep this in mind.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

This is a must read about autoboxing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number Wrapper classes - autoboxing