java方法声明的数据类型与输入值类型不一样会怎么样
比如说声明的是double,输入的是int;或声明的是int,输入的是double在允许的情况下java会自动转化数据类型吗谢谢(●ˇ∀ˇ●)...
比如说声明的是double,输入的是int;或声明的是int,输入的是double
在允许的情况下java会自动转化数据类型吗
谢谢(●ˇ∀ˇ●) 展开
在允许的情况下java会自动转化数据类型吗
谢谢(●ˇ∀ˇ●) 展开
2个回答
展开全部
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);
}
}
这就可以,涉及到自动装箱自动拆箱的问题。
第一种:
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);
}
}
这就可以,涉及到自动装箱自动拆箱的问题。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询