Casting result of multiplication two positive integers to long is negative value

后端 未结 3 1521
时光取名叫无心
时光取名叫无心 2021-01-12 20:27

I have code like this :

int a = 629339;
int b = 4096;
long res = a*b;

The result is -1717194752 but if I add one manual cast

3条回答
  •  走了就别回头了
    2021-01-12 20:51

    long res = a*b;
    

    a*b will be treated as integer unless you add 'l' at end (or) cast.

    As per java tutorial

    The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else.

提交回复
热议问题