• 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

BETA: Method resolving, un/boxing varargs mock question

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys.
For me this are the hardest subject, up to now...
So, just for fun, try to guess which calls will compile and which won�t, and why... Latter, comment the calls which won�t compile and try to guess the output...



I don�t understand why the compiler allows this stuff. I think that the only way to call the 'methods which don�t compile' is using reflection...

Well, post your answers, let�s discuss this hard topic...

Have fun!

Bye...
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eduardo,

Thanks for posting this question.

The compiler determines which method to call by going thru' 3 phases.

Extracted from JLS3 Section 15.12.2:

------------
The first phase (�15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

The second phase (�15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase.

The third phase (�15.12.2.4) allows overloading to be combined with variable
arity methods, boxing and unboxing.

------------

Here's what I think based on the rules above:
//A
method(1); // OK

Without boxing/unboxing and arity methods, the compiler can find an exact method in phase 1, i.e. method(int i).

//B
method(new Integer(1),new Integer(1)); // Compilation error

It isn't able to pass phase 1 since there's no method(Integer x, Integer y). Not for phase 2 too since there's no method(int x, int y), method(int x, Integer y) or method(Integer x, y). For phase 3, any of the following signature combination is possible, hence result in ambiguity during compilation:



Same for D, E, F.

//C
method(new Integer(1)+1);
//G
method(1+new Integer(1));

According to JLS3 5.6.2 Binary Numeric Promotion, the result type of the two operands will be int.

The following program is legal:


After converting to int for both C and G, the compiler can find a matching method in phase 1, i.e. method(int i). Hence compilation passed.

What do you think?
[ January 26, 2005: Message edited by: Joyce Lee ]
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To demonstrate phase 1, here's an example without variable arity-method and it's compiled without error.


method(Integer x, Integer y) is called because it passes the phase 1 test without bothering boxing/unboxing or variable arity-method.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To demonstrate phase 2, removing method(Integer x, Integer) would cause compilation error. That's because boxing/unboxing is at work in phase 2. Any of the following combination is fine for method(new Integer(1), new Integer(1)), hence result in ambiguity during compilation.

 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If method(new Integer(1), new Integer(1)) fails to pass phase 1 and 2, any of the following combination is fine in phase 3. But only one is allowed otherwise ambiguity happens during compilation. Whew!

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic