• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

varargs overloaded method

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works fine:


The output is int

However, this fails to compile (ambiguous method call)


Why the difference between direct call and var-args call??

Thanks.
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here you have overloaded methods in Ex class one which takes var args then you cannot pass a single int or Integer variable to call the method because of this compiler gives you ambiguous error.
Here you have to pass int[] or Integer[] to call particular method.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nidhi Sar wrote:Why the difference between direct call and var-args call??



Boxing + Varargs and Widening + varargs you can do only manually exclusive manner not togather.
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There has been a long discussion on this these days here...
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There has been a long discussion on this these days here...



Thanks for pointing that out. Yes, I have been following that great discussion. In fact, this question is an offshoot of one of the posts in that discussion. Started a new topic, since it seemed a little OT to the original post.

I'm sorry, but can someone please explain this more.

In example 1, there is no widening involved, just boxing... if needed. Since a method call that takes a primitive variable(int) is present, the boxing is not needed either.
In example 2, there are two options - varargs only/ boxing + varargs. If in example 1, the method knows to go to the primitive method call, and not do the boxing needlessly, then why not the same in example 2. Yes, there is varargs also, but again the choice is between primitive and boxing so why not go just to primitive.

The question seems clear enough in my head, but not sure if I am being able to make sense!!!
 
Rajiv Chopra
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nidhi Sar wrote:

There has been a long discussion on this these days here...



Thanks for pointing that out. Yes, I have been following that great discussion. In fact, this question is an offshoot of one of the posts in that discussion. Started a new topic, since it seemed a little OT to the original post.

I'm sorry, but can someone please explain this more.

In example 1, there is no widening involved, just boxing... if needed. Since a method call that takes a primitive variable(int) is present, the boxing is not needed either.
In example 2, there are two options - varargs only/ boxing + varargs. If in example 1, the method knows to go to the primitive method call, and not do the boxing needlessly, then why not the same in example 2. Yes, there is varargs also, but again the choice is between primitive and boxing so why not go just to primitive.

The question seems clear enough in my head, but not sure if I am being able to make sense!!!



Hi you are passing int to an Array argument not to int argument. To create int to Array means you are boxing int in an Array Type Object so it is Boxing + Varargs. Correct me...

 
reply
    Bookmark Topic Watch Topic
  • New Topic