Question

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)

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top