简体   繁体   English

Java-乘法无法按预期工作

[英]Java - Multiplication not working as expected

the below code gives me -1894967296 as result but this is not expected. 下面的代码给我-1894967296作为结果,但是这是不期望的。 What is exactly happening? 到底是怎么回事? I can't figure it out. 我不知道。 The result has to be 2400000000 as per my calculator :( 根据我的计算器,结果必须是2400000000 :(

import java.util.List;
import java.util.ArrayList;
public class Calculate{
     public static void main(String []args){
        int result = 1;
        List<Integer> integer = new ArrayList<Integer>();
        integer.add(100);
        integer.add(200);
        integer.add(300);
        integer.add(400);
        for (Integer value : integer) {
            result *= value;
        }
        System.out.println(result);
     }
}

When I debug, it works correctly till the third iteration. 当我调试时,它可以正常工作直到第三次迭代。

Update: 更新:

As per the answers, the int primitive type range has been exceeded but what will happen to the int variable? 根据答案,已经超出了int基本类型范围,但是int变量将如何处理? It will be defaulted to some value? 它将默认为某个值吗?

The largest number Java can store in an int is 2^31 - 2147483648 Java可以在int存储的最大数量为2 ^ 31-2147483648

2400000000 is larger than that so "wraps around". 2400000000大于那个,所以“环绕”。

You need to use a 64 bit data type such as long . 您需要使用64位数据类型,例如long

2400000000 is too large to store in an int. 2400000000太大,无法存储在int中。 It would be more appropriate to use a long which has a minimum value of -2^63 and maximum value of 2^63 - 1 which your number is well within the range of. 最好使用最小值为-2 ^ 63且最大值为2 ^ 63-1的long ,并且您的数字在此范围之内。

The expected result 2400000000 > Integer.MAX_VALUE . 预期结果2400000000 > Integer.MAX_VALUE So, use long instead of int to store result. 因此,使用long而不是int来存储结果。

Use BigInteger instead of int because the int is limited 使用BigInteger而不是int因为int是有限的

Java doc: Java文件:

int : By default, the int data type is a 32-bit signed two's complement integer, which has a > minimum value of -231 and a maximum value of 231-1. int :默认情况下,int数据类型是32位带符号的二进制补码整数,其最小值> -231,最大值> 231-1。 In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. 在Java SE 8和更高版本中,可以使用int数据类型表示无符号的32位整数,其最小值为0,最大值为232-1。 Use the Integer class to use int data type as an unsigned integer. 使用Integer类可将int数据类型用作无符号整数。 See the section The Number Classes for more information. 有关更多信息,请参见“数字类”部分。 Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers. 静态方法(如compareUnsigned,divideUnsigned等)已添加到Integer类中,以支持无符号整数的算术运算。

That's what most programmer call Pillage. 那就是大多数程序员所说的Pillage。 You shouldn't save a long value inside a small variable, don't expect an Int to save a value 2^31+. 您不应该在小变量内保存长值,也不要期望Int会保存2 ^ 31 +的值。 For what you want to do there's a long 对于您想做的事情,很long

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM