在java中为长数据类型赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18756602/
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
assigning a value to long data type in java
提问by Aarish Ramesh
long val = 5000000000;
The error during this assignment is "The literal 5000000000 of type int is out of range". Why does the compiler by default assumes the literal to be int??
此分配期间的错误是“int 类型的文字 5000000000 超出范围”。为什么编译器默认假定文字是 int?
回答by MansoorShaikh
Add the letter L at the end of your number as shown below
在您的号码末尾添加字母 L,如下所示
long val = 5000000000L;
长 val = 5000000000L;
回答by Juned Ahsan
There is aspecific suffixes for long i.e L
. If there is no suffix, then 5000000000 assumed to be an int
type. And 5000000000 is out of int
range, causing the erro. So you need to add L
at the end of 5000000000 for it to be treated as a long
value. Change your declaration from
long 有特定的后缀,即L
. 如果没有后缀,则假定 5000000000 是一个int
类型。并且 5000000000 超出int
范围,导致错误。因此,您需要L
在 5000000000 的末尾添加才能将其视为long
值。将您的声明从
long val = 5000000000;
to
到
long val = 5000000000L;
回答by Gyanendra Dwivedi
long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
long:long 数据类型是一个 64 位有符号二进制补码整数。它的最小值为 -9,223,372,036,854,775,808,最大值为 9,223,372,036,854,775,807(含)。
You should append 'l' or 'L' to the value explicitly used to initialize the variable; even as small as 0.
您应该将 'l' 或 'L' 附加到显式用于初始化变量的值;甚至小到 0。
long val = 0L;