解析不同的数据类型 Java
Parse different data type Java
因为 int
不如 double
精确,所以我认为在将它解析为方法时需要转换它。然而下面的代码运行良好。为什么?
public class MyClass {
public static void main(String[] args) {
System.out.println(met(3/2));
}
static String met(int i){
return "This is what I get " + i;
}
}
当您执行 3/2
时,不会给您双重结果。整数除法发生并且结果得到 t运行cated 为一个整数。因此不需要强制转换。为了获得双精度结果,两者都需要转换为双精度,以便在将其转换为双精度时出现编译器错误。
尝试 met(3d / 2)
,然后 运行 出现您预期的编译器错误。
因为 int
不如 double
精确,所以我认为在将它解析为方法时需要转换它。然而下面的代码运行良好。为什么?
public class MyClass {
public static void main(String[] args) {
System.out.println(met(3/2));
}
static String met(int i){
return "This is what I get " + i;
}
}
当您执行 3/2
时,不会给您双重结果。整数除法发生并且结果得到 t运行cated 为一个整数。因此不需要强制转换。为了获得双精度结果,两者都需要转换为双精度,以便在将其转换为双精度时出现编译器错误。
尝试 met(3d / 2)
,然后 运行 出现您预期的编译器错误。