Java long assignment confusing

前端 未结 9 1122
一个人的身影
一个人的身影 2021-01-24 05:17

Why does this java code

long a4 = 1L;
long a3 = 1;
long a2 = 100L * 1024 * 1024 * 1024;
long a1 = 100 * 1024 * 1024 * 1024;
System.out.println(a4);
System.out.pr         


        
9条回答
  •  花落未央
    2021-01-24 06:08

    Here's what you did : You assigned 100 * 1024 * 1024 * 1024 to a long data type but you didn' said that 100 * 1024 * 1024 * 1024 is a long value

    By default java compiler thinks that its an integer. Since integer cannot hold that much value, it will show wrong result. Hope it helps !

提交回复
热议问题