• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Method Invocation Conversions (compiler decision rules??)

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

As this is my first post, I will introduce myself. I'm Thyago and I'll take the SCJP exame soon because after two years I decided to came back to Java. I hope I can share with you interesting questions whose answers I don't know ... yet )

When it comes to primitives and wrappers, in order to determine which overloaded method will be invoked one applies the rules below:
- primitive widening has priority over boxing
- var-args method has the least priority

But are these priorities applied argument by argument by the compiler? I don't know the priority order for the following overloaded methods when one calls the method passing five bytes go(b1,b2,b3,b4,b5):

1) Does the compiler count the arguments that mathes 100% or simply eliminate the answer where at least one parameter need to be widen to a larger container?
A.void go(short a, short b, short c, short d, short e){ }
B.void go(byte a, byte b, byte c, byte d, int e){ }

2) Same question but with boxing
A.void go(short a, short b, short c, short d, short e){ }
B.void go(byte a, byte b, byte c, byte d, Byte e){ }

3) Does the order of arguments matter? In this situation both has boxing but in a different order
A.void go(Byte a, byte b, byte c, byte d, byte e){ }
B.void go(byte a, Byte b, byte c, byte d, byte e){ }

4) Now if I mix widening reference objects and primitives, which one has the priority? Suppose Dog extends Animal and the variables Dog d and byte b and the call test(d,b), which overloaded method is picked up by the compiler?
A.void test(Animal a, byte b){ }
B.void test(Dog d, int b){ }

If some one knows the exact compiler rules to determine the overloaded method to be called(and more importantly why) ...

If there's no way to know please indicate if the exam expects me to know it

Thanks in advance
-Thyago Consort
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thyago

Welcome to JavaRanch!

There is a good blog by Corey on this topic.

Once you read that it should be easy for you to understand the compiler behavior in over-loaded methods.

Murali...
 
Everybody's invited. Except this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic