aspose file tools*
The moose likes Java in General and the fly likes JDK 1.5 AutoBoxing Confusion in Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "JDK 1.5 AutoBoxing Confusion in Code " Watch "JDK 1.5 AutoBoxing Confusion in Code " New topic
Author

JDK 1.5 AutoBoxing Confusion in Code

Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi Folks ...

I am basically confused on below code , how autoboxing works


My Question is why i dint get Ambiguous method declared error at compile time

Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39804
    
  28
Because it is obvious to the compiler that you are passing a primitive in your first call and a wrapper in the second call.
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Campbell Ritchie wrote:Because it is obvious to the compiler that you are passing a primitive in your first call and a wrapper in the second call.


Dear Campbell ,

Thanks for reply .

How the method mtd() is called autoboxing happends automatically ? , why not for this num(primitive) and num(wrapper) and confusion wont happend ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

Ganesh Gowtham wrote:
How the method mtd() is called autoboxing happends automatically ? , why not for this num(primitive) and num(wrapper) and confusion wont happend ?


Put yourself in the place of the compiler. In the case of the mtd() method, you only have one option, and the only way to get to Integer, when given an int is to autobox. In the case of num(), you have two options, and two perfect matches. Why bother looking into the option of boxing when you have a perfect match?

Or to answer your question... the compiler just doesn't try everything. It has an order, and have ways to resolve ambiguous issues too.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
In your example, only the last call uses auto-boxing; new Integer(6) is passed to mtd().
Don't over-think this auto-boxing thing.

Jim...


BEE MBA PMP SCJP-6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Jim Hoglund wrote:In your example, only the last call uses auto-boxing; new Integer(6) is passed to mtd().

Almost right. It's Integer.valueOf(6), not new Integer(6).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Ahhhh... I see oh wise one...

Integer.valueOf(): Returns an Integer instance representing the specified int
value. If a new Integer instance is not required, this method should generally
be used in preference to the constructor Integer(int), as this method is likely
to yield significantly better space and time performance by caching frequently
requested values.

But this leads to the question, when would a second Integer instance be
needed to represent 6? Would this be never?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

It sure would. Whenever you call Integer.valueOf for any value between -128 and 127 (inclusive) it caches objects for all these values. Since Integer is immutable you should just reuse these objects.
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Dear all,

I was basically convinced after seeing the decompiled code which transformed Integer to int value

Thanks a lot your time .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDK 1.5 AutoBoxing Confusion in Code