简体   繁体   中英

invocation order of overloaded methods in JAVA

I'm studying some exams of java and I came across with this question:

//Write the output of this program:

public static void method(Integer i) { System.out.println("Integer"); }
public static void method(short i) { System.out.println("short"); }
public static void method(long i) { System.out.println("long"); }
//...
public static void main(String []args) {
method(10);
}

//ANSWER: long

The explanation describes that for an integer literal, the JVM matches in the order: int, long, Integer. Since there is no method with int type parameter, then looks for long type; and so on.

In this explanation they only provide the order for int, long and Integer. so my question is: What is the complete order list when an integer literal is introduced in a method that is overloaded for every type (that uses integers)?

Also, what is the order for float, double etc...?(decimal values)

The complete list might be int, long, float, double, Integer, Number/Comparable/Serializable, Object.

Note: the Number, Comparable, and Serializable are ambiguous. An explicit cast would be needed to pick one of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM