• 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

confusion in methdo overloading with var args

 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

produces output-

in int

I have some questions regarding this code-

1. Why the call to method A is not ambigous?

2. Priority order for method call is Widening > Boxing > Varargs. Then why unboxing is prefered over widening here?
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) When line 13 is commented out, the two overloaded methods are not ambiguous as one takes Object while the other one takes int.
2) When line 13 is uncommented out and executed, int... beats Object ... because unboxing costs less time to do than widening. But this is just my guess.

A(int...x) is equivalent to A(in[] x)
A (Object... x) is equivalent to A(Object[] x)

I think unboxing a set of Integer object, create an array of int cost less time for the compiler to do.
On the other hand, widening a set of Integer object, means creating an array of Object , and assign the Integer object as each element in this Object array, cost more time to do.

I hope someone else can help.

 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, here compiler choosing the most nearest matched object in the hierarchy, means Integer extends Object, and for "int" argument Integer matches first and thus it get called.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Helen and Sagar for the reply.
I'm getting confused because it is not following the priority order Widening > Boxing > Varargs.
Priority of widening is greater than boxing. So, shouldn't it be greater than unboxing also? Why compile is ignoring the priority order here?
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess unboxing has higher priority than widening in this case.

Can anyone help?
 
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

Astha Sharma wrote:Thanks Helen and Sagar for the reply.
I'm getting confused because it is not following the priority order Widening > Boxing > Varargs.
Priority of widening is greater than boxing. So, shouldn't it be greater than unboxing also? Why compile is ignoring the priority order here?



I am more interested to know where you got the idea of priority of "Widening > Boxing > Varargs" -- as I don't recall ever seeing anything in the JLS that mentions this........ actually, admittedly, I find the section about method overloading, the "most specific" rule, and how it is related to variable arity to be very confusing.

Henry
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:I am more interested to know where you got the idea of priority of "Widening > Boxing > Varargs" -- as I don't recall ever seeing anything in the JLS that mentions this........ actually, admittedly, I find the section about method overloading, the "most specific" rule, and how it is related to variable arity to be very confusing.


It is written in k&b chapter 3, Assignment, page number 250

Widening beats boxing
Widening beats var-args
boxing beats var-args


Also see the thread
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic