• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDK 1.5 AutoBoxing Confusion in Code

 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

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

Thanks a lot your time .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic