• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

re: ambiguous method calls

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could use some clarification on this issue...according to Q10 on http://www.javabeat.net/javabeat/scjp5/mocks/BoxingConversion-10questions.php, the following code compiles fine...

public class Boxing10 {
public static void main(String[] args) {
int i = 10;
int k = 20;
method(i,k);
}
static void method(Integer... i){
System.out.println("Integer varargs is called");
}
static void method(Integer i,Integer j){
System.out.println("Integer,Integer is called");
}
static void method(int... i){
System.out.println("int varargs is called");
}
}

However, based on some other mocks this seems wrong. According to the Inquisition program I downloaded, the following code won't compile.

class test {
public static void main(String[] args) {
test inst_test = new test();
inst_test.method ( 1 , 1 , 1);
inst_test.method( new Integer(1) , new Integer(2) , new Integer(3) );
inst_test.method ( 1 , new Integer(5) );
inst_test.method ( new Integer(10) , 1 );
}
public void method( Integer... I ){
System.out.println("Eye in the sky");
}
public void method( int... i ){
System.out.println("Fly in the pie");
}
}

Explanation given is "To the compiler Integer... and int... are pretty much the same. This results in an ambiguous state, which results in an error. The JVM wont know which method to call if this is allowed to be compiled." It seems like I have seen a similar complaint about ambiguous calls on other questions and material. Which one is correct? Does it have to do with the first example using static methods?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it's not because they are static.

The reason is that in order to make the new JVMs compatible with older code, method matching is done in phases.

The first phase is to see if there are any matches without using boxing or varargs.

If there are none, then the second phase is to see if there are any matches using boxing but not varargs.

If there are none, then the third phase is to see if there are any matches using boxing and varargs.

In the first example, there is match using boxing.

In the second example, you have two methods which will match the call but neither is more specific than the other.
 
Ivan Karamazov
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind, just realized what was going on. I need to take a break.

EDIT - Thanks for the reply, that got slipped in quickly.
[ April 05, 2007: Message edited by: Ivan Karamazov ]
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about this rxample , why its give same error with int...

as i know it will choose the narrowest method which is Integer not long or float or double !

 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic