public class HelloWorld {
public static void main(String[] args) {
long ageEarth = 4543000000L;
System.out.println(ageEarth);
double ageEarth1 = ageEarth;
System.out.println(ageEarth1);
*ageEarth1 = (long) ageEarth1;
System.out.println(ageEarth1)*
}
Playing周围的值为[[4543000000,然后将其传递给double,然后再次传递给long给我,
科学记法:4.543E9
但是科学计数法是点浮点数,而long只接受整数,这是怎么回事?
ageEarth1
是double
类型,而不是long
。通过强制转换将其转换为long
会截断它,但是随后将其存储为double
,因此它的行为就像使用时一样,因为它是double
。
如果将其转换回并将数据存储为long
,您会看到它按预期打印:
long ageEarth2 = (long)ageEarth1;
System.out.println(ageEarth2);