声明带有"L"和不带有"L"的长变量有什么区别? [英] What's the difference between declaring a long variable with 'L' and without?
问题描述
以下内容有什么区别?
long var1 = 2147483647L;
long var2 = 2147483648;
(或任何原始变量声明)带有或不带有L时,是否存在任何性能问题?是强制性的吗?
(or any primitive variable declaration) Does it have any performance issue with or without L? Is it mandatory?
推荐答案
在第一种情况下,您要为 long
变量(L
或 l
后缀表示 long
类型.
In the first case you are assigning a long
literal to a long
variable (the L
or l
suffix indicates long
type).
在第二种情况下,您要为 long
变量分配一个 int
文字(在不提供后缀的情况下为默认类型)(这会导致从 int
到 long
),这意味着您被限制在从 Integer.MIN_VALUE
到 Integer.MAX_VALUE
的范围内 -2147483648
到 2147483647
).
In the second case you are assigning an int
literal (that's the default type when no suffix is supplied) to a long
variable (which causes an automatic type cast from int
to long
), which means you are restricted to the range from Integer.MIN_VALUE
to Integer.MAX_VALUE
(-2147483648
to 2147483647
).
这就是原因
long var2 = 2147483648;
不通过编译( 2147483648
大于 Integer.MAX_VALUE
).
另一方面
long var2 = 2147483648L;
将通过编译.
这篇关于声明带有"L"和不带有"L"的长变量有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!