查看下面的源代码
class Test
{
public static void main(String[] args)
{
Double object = new Double("2.4");
int a = object.intValue();
byte b = object.byteValue();
float d = object.floatValue();
double c = object.doubleValue();
System.out.println(a + b + c + d );
}
}
输出:无 错误 您可能分配了过多的内存
谁能详细解释
答案 0 :(得分:0)
将代码更改为
Double object = new Double("2.4");
int a = object.intValue();
byte b = object.byteValue();
float d = object.floatValue();
double c = object.doubleValue();
System.out.println(a);
System.out.println(a + b);
System.out.println(a + b + c);
System.out.println(a + b + c + d );
并探索
答案 1 :(得分:0)
万一有人还在寻找答案,为什么会有15位小数的精度。
public class JavaExample {
public static void main(String[] args) {
Double object = new Double("2.4");
int a = object.intValue();
byte b = object.byteValue();
float c = object.floatValue();
double d = object.doubleValue();
float e = object.floatValue();
double f = object.doubleValue();
System.out.println("int :"+a);
System.out.println("int and byte addition :"+(a + b));
System.out.println("int, byte and float addition :"+(a + b + d));
System.out.println("int, byte, float and double addition :"+(a + b + c + d));
System.out.println("float and double addition :"+(c + d));
System.out.println("float and float addition :"+(c + e));
System.out.println("double and double addition :"+(d + f));
System.out.println("float and double addition :"+(e + f));
}
}
Output:
int :2
int and byte addition :4
int, byte and float addition :6.4
int, byte, float and double addition :8.800000095367432
float and double addition :4.800000095367432
float and float addition :4.8
double and double addition :4.8
float and double addition :4.800000095367432