• 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

Var-Args problem

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: http://www.examlab.tk/ and Practice Exam: 2 Question No:33


If I uncomment the above commented lines, code does not compile. With comments, it compiles just fine. That goes to say that overloading is fine here. My question is whether there is any way to call any of these two methods successfully.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garima,
Overloading, compilation wise, is fine here. But usage wise, its not fine. Because, at the end, you are unable to call these methods because of the ambiguity issue, as there is varargs in one call and the other is boxing+varargs...

One way of solving this issue would be to change the first callMethod to public static void callMethod(Integer i){ (removal of varargs syntax)...that way, you will get Primitive as the output.

Another option is to remove the varargs from the second call. Or else, in the parameters you are passing, do not pass variables that involve unboxing...

Solutions are many...But, with the same exact code you have given, I don't think you can call those methods.
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rekha...
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt know about that ambiguous error, what a tricky for the exam heh
 
reply
    Bookmark Topic Watch Topic
  • New Topic