• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Method Resolution

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

In the following code,
public class Test {
public static vois main(String args[]){
flipFlop("(String, Integer, int)", new Integer(4), 2004); //1
}
private static void flipFlop(String str, int i, Integer iRef) { //2
out.println(str + " ==> (String, int, Integer)");
}
private static void flipFlop(String str, int i, int j) { //3
out.println(str + " ==> (String, int, int)");
}
}
Why is the method call at 1 gives compiler error? Is it not the method at 3 more specific than method at 2 ? I can find the arguments (String str, int i, byte b ) that can be passed to the method at 3 but not for the method at 2 but I cannot find any arguments that are valid for method 2 and not for 3.

regards
praveen
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static vois main(String args[]){
flipFlop("(String, Integer, int)", new Integer(4), 2004); //1
there is no method definition which is satisfying your parameters ,so error came, in method 2 your formal parameters are string,int,Integer

so write a method with formal parameters String,Integer,int,
first two are string and integer class reference variables ,last is the integer data type.
 
praveen vempati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess, my question is not clear. If I comment any one of the two methods, then the code compiles fine. However, when both methods are there, the compiler complains that the method call is ambiguous. My question is how can we determine whether a method is more specific than the other?

regards,
Nanda Kishore
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Selecting the Most Specific Method
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, u said " I can find the arguments (String str, int i, byte b ) that can be passed ". but u have to remember that any literal which is a number is of the type int. and any decimal literal is of the type double. so when u write any literal for ex, 10, 200. they are all ints and not bytes or shorts. so ur method invocation of (String str, int i, byte b )is wrong. and your method invocation by the compiler wil be(String str, int i, int b ).

Amarendra k
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/326225/java/java/Ambiguous-Reference-method
may be this will help
 
praveen vempati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, I got it.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Y following code compiles correctly...


Regards

Naseem.K
 
praveen vempati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naseem,
that's because of boxing and unboxing. The second argument Integer will be unboxed to int and the third argument which is an int literal will be boxed to an Integer

regards
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great catch...Thanks Praveen

I Compiled this code on j2sdk 1.2.1 and this is the compilation error output....
Incompatible type for method. Can't convert java.lang.Integer to int and int to java.lang.Integer.
I don't know wat will be output on j2sdk 1.4 version.
I think boxing and unboxing is the feature of 1.5, dats y compiler compiles without any error output.
Same is the problem in ur code which u have mentioned in start. It can box and unbox in either way, dats y it says ambiguous call.

regards

Naseem
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic