• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

i am confusing about Overloading

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B p240:compiler choose old style before it choose new one,
but this get me confusing,how about this output?

public class Confusing {
     private Confusing(Object o) {
         System.out.println("Object");
     }

// private Confusing(double[] dArray) {
// System.out.println("double array");
// }
     private Confusing(double...ds ){
         System.out.println("double ...");
     }

     public static void main(String[] args) {
         new Confusing(null);
     }
}

old beat vararg? No.
//output
double ...

Somebody help!

Best,
Yue

[ December 28, 2006: Message edited by: YUE ZHU ]

[ December 28, 2006: Message edited by: YUE ZHU ]
[ January 10, 2007: Message edited by: YUE ZHU ]
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A method is said to be maximally specific for a method invocation if it is applicable and accessible and there is no other applicable and accessible method that is more specific.

If there is exactly one maximally specific method, then it is in fact the most specific method; it is necessarily more specific than any other method that is applicable and accessible.

It is possible that no method is the most specific, because there are two or more maximally specific method declarations. It's not possible to identify a most-specific method from among the applicable methods. In this case, we say that the method invocation is ambiguous, and a compile-time error occurs.

The precise definition of the most specific property is as follows. Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T1, . . . , Tn; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U1, . . . , Un . Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:

T can be converted to U by method invocation conversion.
Tj can be converted to Uj by method invocation conversion, for all j from 1 to n.

A slight change to your program,



And an ambiguity error results:
javac Confusing.java
Confusing.java:14: reference to Confusing is ambiguous, both method Confusing(java.lang.Double) in Confusing and method Confusing(double...) in Confusing match new Confusing(null);
^
1 error

 
YUE ZHU
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Aniket. Your explanation is clear.

Best,
Yue
 
YUE ZHU
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I run this fine,and outputs:
long


but,when I add a parameter more, it's go wrong:

It shows ambiguous, I don't know why? Anyone help?

Best,
Yue
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mhhh... if you change the calling code to f(5,5,5,new Integer(5)) it works... Could someone explain this behavior please? I'm confused, too.
 
YUE ZHU
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bug of Java Compiler? :-)

Anyone help?

Best,
Yue
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic