如果实参是基本数据类型:先找带有该类型参数的方法,找不到则扩大范围继续找,如果还是没有才会去找包装类; 如果实参是包装类型:优先找有该包装类型的方法,如果没有匹配,则会找父类,还找不到就会将包装类拆箱匹配。 下面例子控制台会输出“Integer”,如果注释掉
void method(Integer i){ System.out.println(“Integer”); }
就会输出“Object”;
public class Test {
void method(int i){ System.out.println("int"); } void method(Integer i){ System.out.println("Integer"); } void method(Object i){ System.out.println("Object"); } void method(float i){ System.out.println("float"); } void method(double i){ System.out.println("double"); } public static void main(String\[\] args) { Test test = new Test(); test.method(new Integer(2)); }
}