Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 968
Hugo Medina Avatar asked Oct 08 '13 18:10

Hugo Medina


1 Answers

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.

like image 139
Peter Lawrey Avatar answered Oct 13 '22 15:10

Peter Lawrey