aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes BETA: Method resolving, un/boxing varargs mock question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "BETA: Method resolving, un/boxing varargs mock question" Watch "BETA: Method resolving, un/boxing varargs mock question" New topic
Author

BETA: Method resolving, un/boxing varargs mock question

Eduardo Rodrigues
Ranch Hand

Joined: Jul 01, 2003
Posts: 199
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...


Eduardo Rodrigues<br />SCJP 1.4/5.0 SCWCD 1.3/1.4, SCBCD 1.3, SCMAD, SCEA<br />IBM 484 & 486<br />Belo Horizonte<br />Minas Gerais<br />Brasil
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Jul 11, 2003
Posts: 1392
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!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BETA: Method resolving, un/boxing varargs mock question
 
Similar Threads
re: ambiguous method calls
AutoBoxing
instance initializer
Overloading Ambiguously
overloading inherited method