为什么强制转换此变量给我一个包含科学符号的长数据类型?

问题描述 投票:-1回答:1
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只接受整数,这是怎么回事?

java casting
1个回答
1
投票
[ageEarth1double类型,而不是long

通过强制转换将其转换为long会截断它,但是随后将其存储为double,因此它的行为就像使用时一样,因为它是double

如果将其转换回并将数据存储为long,您会看到它按预期打印:

long ageEarth2 = (long)ageEarth1; System.out.println(ageEarth2);

© www.soinside.com 2019 - 2024. All rights reserved.