javaargumentsoverloading

Where are the preferred argument conversions for overloaded methods documented (if at all)?


In other words, what are the precise rules for how the Java compiler determines which overloaded method to choose to execute? I've spent a good amount of time googling and I think I'm not using the right search keywords.

public class C1  extends C2 {}
public class C2  extends C3 {}
public class C3 {}

public class Test {

    public static void main(String[] args) {
        C1 c1 = new C1();
        // What are the precise rules for determining 
        // which method below is called?
        method(c1, c1); 
    }

    static public void method(C3 test, C3 test2) {
        System.out.println("C3");
    }

    static public void method(C2 test, C3 test2) {
        System.out.println("C2");
    }

}

Solution

  • The relevant part of the JLS is 15.12.2 Compile-Time Step 2: Determine Method Signature. The rules are complicated and technical, but the general principle is that the applicable method with the most specific argument types is chosen.