• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JDK 1.5 AutoBoxing Confusion in Code

 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic