java方法声明的数据类型与输入值类型不一样会怎么样
发布网友
发布时间:2023-03-05 02:04
我来回答
共2个回答
热心网友
时间:2024-12-14 17:28
java方法声明的数据类型与输入值类型不一样
第一种:
public class CovTest {
private int a;
private double b;
public void display(int c, double d){
System.out.println(c+",,,"+d);
}
public static void main(String[] args) {
CovTest test = new CovTest();
test.display(2.4, 5); //这里会报错
//The method display(int, double) in the type CovTest is not applicable for //the arguments (double, int) 类型不一致的错误
}
}
第二种:
public class CovTest {
private int a;
private double b;
public void display(Integer c, Double d){
System.out.println(c+",,,"+d);
}
public static void main(String[] args) {
CovTest test = new CovTest();
int c = 10;
double d = 2.5;
test.display(c, d);
}
}
public class CovTest {
private int a;
private double b;
public void display(int c, double d){
System.out.println(c+",,,"+d);
}
public static void main(String[] args) {
CovTest test = new CovTest();
Integer c = 10;
Double d = 2.5;
test.display(c, d);
}
}
这就可以,涉及到自动装箱自动拆箱的问题。
热心网友
时间:2024-12-14 17:28
默认是会向上转型的,向下的话需要强制转型或者调用函数