Java 乘以长值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1494862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiplying long values?
提问by Jonhnny Weslley
class Main {
public static void main (String[] args){
long value = 1024 * 1024 * 1024 * 80;
System.out.println(Long.MAX_VALUE);
System.out.println(value);
}
}
Output is:
输出是:
9223372036854775807 0
It's correct if long value = 1024 * 1024 * 1024 * 80L;
!
如果是对的long value = 1024 * 1024 * 1024 * 80L;
!
采纳答案by Erich
In Java, all math is done in the largest data type required to handle all of the current values. So, if you have int * int, it will always do the math as an integer, but int * long is done as a long.
在 Java 中,所有数学运算都以处理所有当前值所需的最大数据类型完成。因此,如果您有 int * int,它将始终以整数形式进行数学运算,但 int * long 以 long 形式进行。
In this case, the 1024*1024*1024*80 is done as an Int, which overflows int.
在这种情况下,1024*1024*1024*80 作为一个 Int 完成,它会溢出 int。
The "L" of course forces one of the operands to be an Int-64 (long), therefore all the math is done storing the values as a Long, thus no overflow occurs.
“L”当然会强制操作数之一为 Int-64(长整型),因此所有数学运算都已完成,将值存储为 Long,因此不会发生溢出。
回答by Tom Hawtin - tackline
The integer literals are int
s. The int
s overflow. Use the L
suffix.
整数文字是int
s。在int
小号溢出。使用L
后缀。
long value = 1024L * 1024L * 1024L * 80L;
If the data came from variables either cast or assign to longs beforehand.
如果数据来自变量,则事先将其强制转换或分配给 long。
long value = (long)a * (long)b;
long aL = a;
long bL = b;
long value = aL*bL
Strictly speaking you can get away with less suffices, but it's probably better to be clear.
严格来说,你可以用更少的东西逃脱,但最好是清楚的。
Also not the lowercase l
as a suffix can be confused as a 1
.
也不是小写l
作为后缀可以混淆为1
.
回答by developmentalinsanity
I suspect it's because by default java treats literals as integers, not longs. So, without the L on 80 the multiplication overflows.
我怀疑这是因为默认情况下 java 将文字视为整数,而不是长整数。因此,如果没有 80 上的 L,则乘法溢出。
回答by Pete Kirkham
This code:
这段代码:
long value = 1024 * 1024 * 1024 * 80;
multiplies some integers together, converts it to a long and then assigns the result to a variable. The actual multiplication will be done by javac rather than when it runs.
将一些整数相乘,将其转换为 long,然后将结果分配给变量。实际的乘法将由 javac 完成,而不是在它运行时完成。
Since int is 32 bits, the value is wrapped and results in a zero.
由于 int 是 32 位,因此该值被包装并产生零。
As you say, using long values in the right hand side will give a result which only wraps if it exceeds 64 bits.
正如您所说,在右侧使用长值会给出一个结果,该结果仅在超过 64 位时进行换行。
回答by Ash_P
If I understood it correctly, per your requirement, you wanted to multiply these values 1024 * 1024 * 1024 * 80;
如果我理解正确,根据您的要求,您想乘以这些值 1024 * 1024 * 1024 * 80;
In calculator I see values coming 1024 * 1024 * 1024 * 80=85899345920.
在计算器中,我看到值是 1024 * 1024 * 1024 * 80=85899345920。
Here you go your java code: These are monetary value calculation in java
这是你的java代码:这些是java中的货币价值计算
import java.math.BigDecimal;
public class Demo {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("1024");
BigDecimal bd2 = new BigDecimal("1024");
BigDecimal bd3 = new BigDecimal("1024");
BigDecimal bd4 = new BigDecimal("80");
BigDecimal bd5 = bd1.multiply(bd2).multiply(bd3).multiply(bd4);
System.out.println(bd5); // output comes: 85899345920
}
}