This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading with var-args Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading with var-args" Watch "Overloading with var-args" New topic
Author

Overloading with var-args

Chris Hatton
Greenhorn

Joined: May 26, 2008
Posts: 11
Hi all

I'm new to JavaRanch so might be asking some daft questions to start with. I noticed that the following code does not compile because the call to m1() is ambiguous. Looking at a previous post: '(var-arg, Boxing, Widening) ambiguous call error' the conclusion was that this was a bug in the compiler.

I know that it should compile and that the m1(int... i) method should be called as it is the most specific method. Now question that I wanted to ask is, if in the exam I'm asked if a piece of code like this compiles. Do I answer yes because Java Language Specification says it should or do I answer no because in reality it doesn't? Would a question like this even be asked in the exam? Presumably this a well known problem by now.


public class OverloadingTest {

public static void main(String[] args) {
int i = 1;

m1(i);
}
static void m1(int... i){
System.out.println("int... method");
}
static void m1(long... i){
System.out.println("long... method");
}
}

Thanks


Chris
Jalees Ahmed
Greenhorn

Joined: Jul 10, 2008
Posts: 3
Hi....

I have compiled and executed your code.
The code you send successfully compiled and running the m1(int i) version of the method.
Where is the problem?
Chris Hatton
Greenhorn

Joined: May 26, 2008
Posts: 11
Are you using the standard javac compiler supplied by Sun? Because it is definitely not working for me.

C:\Program Files\Java\jdk1.5.0_16\bin>javac OverloadingTest.java
OverloadingTest.java:6: reference to m1 is ambiguous, both method m1(int...) in
OverloadingTest and method m1(long...) in OverloadingTest match
m1(i);
^
1 error
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
I've got the same results as you, Chris, and I'm using java version 1.6.0_06. I don't remember reading that the int version should be chosen, but I'll have to go back and review chapter 3 in the K&B book.


Studying for SCJP 6
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Can someone guide us through the JLS with this example? I can see that both functions are applicable and that the step "15.12.2.5 Choosing the Most Specific Method" is reached, but I can't make enough sense out of it.


Java hobbyist.
Lino Larios
Greenhorn

Joined: Jun 27, 2008
Posts: 28
I tried these code and i got the same compile error

Why ?...
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
I'm still waiting for an answer to this question... I couldn't understand the java language spec regarding this issue. For some reason, when using
this is a compiler error, but when using

the m1(int i) method is called. Why is this?
[ July 29, 2008: Message edited by: Eric Daly ]
Chris Hatton
Greenhorn

Joined: May 26, 2008
Posts: 11
Previous discussion on the same topic

explains that it is a compiler bug:

Bug ID: 6199075 Unambiguous varargs method calls flagged as ambiguous

I don't have time or the inclination to learn the notation and grammar of the JLS just now but sufficed to say that the compiler should be able pick the most specific method in that code. That still leaves my original question unanswered but an intelligent guess would say that the creators of the exam knew about this and wouldn't put a question like in the exam.
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Fair enough. I'd agree that this wouldn't be on the question, because I would think (well, I assume) that they actually test the questions they ask in an actual compiler. Thanks for the response, I can put my mind at ease
 
wood burning stoves
 
subject: Overloading with var-args
 
Similar Threads
Exam Lab Test 2 Question
varargs overloaded method
Var-Args question
Var args ... explanation needed
Why is the compiler confused?