看起来像是整数溢出。
看看这个
BigDecimal product=new BigDecimal(1);
for(int i=10;i<99;i++){
product=product.multiply(new BigDecimal(i));
}
System.out.println(product);
输出:
25977982938941930515945176761070443325092850981258133993315252362474391176210383043658995147728530422794328291965962468114563072000000000000000000000
输出不再是int
值。然后,由于溢出,您将获得错误的值。
如果溢出,它将返回最小值并从那里继续。如果下溢,它将返回最大值并从那里继续。
更多资讯
编辑。
让我们如下更改代码
int product = 1;
for (int i = 10; i < 99; i++) {
product *= i;
System.out.println(product);
}
输出:
10
110
1320
17160
240240
3603600
57657600
980179200
463356416
213837312
-18221056
-382642176
171806720
-343412736
348028928
110788608
-1414463488
464191488
112459776
-1033633792
-944242688
793247744
-385875968
150994944
838860800
-704643072
402653184
2013265920
-805306368
-1342177280
-2147483648
-2147483648>>>binary representation is 11111111111111111111111111101011 10000000000000000000000000000000
0 >>> here binary representation will become 11111111111111111111111111101011 00000000000000000000000000000000
----
0