Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading with var-args

 
Chris Hatton
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lino Larios
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried these code and i got the same compile error

Why ?...
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic