类型提升是方法重载,如果没有找到精确匹配,可以隐式地将一种数据类型提升为另一种数据类型。
如上图所示,byte
可以提升为 short
、int
、long
、float
或 double
。 short
类型可以提升为 int
、long
、float
或 double
。 char
数据类型可以提升为 int
、long
、float
或 double
等。 考虑以下示例:
class OverloadingCalculation1 {
void sum(int a, long b) {
System.out.println(a + b);
}
void sum(int a, int b, int c) {
System.out.println(a + b + c);
}
public static void main(String args[]) {
OverloadingCalculation1 obj = new OverloadingCalculation1();
obj.sum(20, 20);//now second int literal will be promoted to long
obj.sum(20, 20, 20);
}
}
运行结果:
40
60