Java long assignment confusing

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

    As per the documentation of Lexical Literals it is mentioned that,

    The type of a literal is determined as follows:
    - The type of an integer literal (§3.10.1) that ends with L or l is long (§4.2.1).
    - The type of any other integer literal is int (§4.2.1).

    Thus your expression, 100 * 1024 * 1024 * 1024 is evaluated as int primitive data type because l or L is not mentioned in any numeric value. and the result is 107374182400 i.e. in Binary it is 1 1001 0000 0000 0000 0000 0000 0000 0000 0000 and int is 32-bit so low 32-bit are taken as mentioned in Example 4.2.2-1. Integer Operations which results to 0

    It is also mentioned in same documentation that,

    If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion

    It means any value in expression contains l or L then all the int values will be expanded to 64 bit.

    EDIT In the comment it is also asked

    Why doesn't it go negative?

    I think it also answers the above question

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-24 05:58

    Number 3 worked because you specified a long type which is 100L. Thats why it is a long multiplication and could be stored. On the other hand number 4 is an integer multiplication with max value 2^32-1 thats why you got an overflow and the zero default valued appeared.

    0 讨论(0)
  • 2021-01-24 06:02

    107374182400 is exactly 25 times the full range of an integer (2^32), which means that if you try to fit it into an integer it will overflow. And because it would fit exactly 25 times it ends up precisely on 0 (this is a coincidence and other huge multiplications may end up either positive or negative). And you are using an integer right up to the point you cast to long

    long a1 = 100 * 1024 * 1024 * 1024;
    

    is equivalent to

    int temp = 100 * 1024 * 1024 * 1024;
    long a1 = (long)temp;
    

    If you put a single long in the expression, it is forced to use long maths not integer maths which removes the problem

    0 讨论(0)
  • 2021-01-24 06:05

    In Java, if you have int * int, it will compute the output as an int. It only gives the result as a long if you do int * long. In your case, the 100 * 1024 * 1024 * 1024 has a result that overflows int.

    So, adding a "L" makes the operand to be a long, and the computation stores the values in long. And of course, no overflow occurs and a correct result can be outputed (i.e. a2).

    0 讨论(0)
  • 2021-01-24 06:06

    It might be that the expression on the right of a1 is first calculated as an int and later on converted to long. If it equals 0 as an int it'll stay 0 as a long

    0 讨论(0)
提交回复
热议问题