• 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

overloading

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



why this will not complie ? aren;t this overloading?
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have asked same question this may help
 
Ranch Hand
Posts: 808
1
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 we have a little different question.
 
Heba Mahmoud
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yess really there was a diiferent as the complair deals the Integer...i as int...i

i get this question from mock exam and it have an answer tht said " when using variable length arquments you can not pass a primitve /wrapper to an overloaded method, which has both primitive var-args and wrapper var-args as their overloaded version"
but i don't understand that answer
 
Sheriff
Posts: 9707
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
Can you please Quote Your Sources.

The explanation seems quite clear to me. If you have overloaded methods in a class one which takes int... and another which takes Integer..., then you cannot pass a single int or Integer variable to call the method (the call will be ambiguous). If you pass int[] or Integer[], then you can call the methods like this

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Can you please Quote Your Sources.

The explanation seems quite clear to me. If you have overloaded methods in a class one which takes int... and another which takes Integer..., then you cannot pass a single int or Integer variable to call the method (the call will be ambiguous). If you pass int[] or Integer[], then you can call the methods like this


It's a question on ExamLab, the diagnostic test - I just took it myself and remember this one.

You're explanation is right, you can't pass a single int or Integer...but you -have- to pass an array of one or the other. For instance:


Only line 6 will compile (assuming the methods are the same).
 
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why? I still don't understand.

What happens if you pass an int?
 
Vince Kennedy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chandella Montero wrote:Why? I still don't understand.

What happens if you pass an int?



It's a compile error. It's been a little while since I've been through this completely, so double check my explanation.

If you have a a method that takes an Integer... (var args of Integer Objects), passing an int (I believe) is fine since you have wrappers.

If you have a method that takes an int...(primitive var args), passing the primitive is again fine - just a different number of var args.

But if you have both, I believe both are viewed as 'objects' of sorts (Integer Array and int array) and the compiler finds the call ambiguous.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic