Java long assignment confusing

前端 未结 9 1221
一个人的身影
一个人的身影 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 05:53

    reason is integer overflow
    as output of 100 * 1024 * 1024 * 1024; is an integer(int) not long

    and in long a2 = 100L * 1024 * 1024 * 1024; you are specifying that one of the value is long (here 100L) and multiplication with that value results in long value which gets correctly stored in a2

提交回复
热议问题