[英]Java: Is there a difference between L and l (lowercase L) when specifying a long?
When I specify a number to be a long
with a constant value 400
, is there any difference between using 400L
and 400l
?当我将一个数字指定为具有常量值
400
的long
时,使用400L
和400l
之间有什么区别吗?
Does it have some relationship to the wrapper type?它与包装器类型有什么关系吗? Is
L
used to get a wrapper Long
and l
for the primitive data type long
? L
是否用于获取原始数据类型long
的包装器Long
和l
?
No practical difference. 没有实际差别。 Either
L
or l
can be used, both indicate a long
primitive. 可以使用
L
或l
,两者都表示long
基元。 Also, either can be autoboxed to the corresponding Long
wrapper type. 此外,任何一个都可以自动装箱到相应的
Long
包装器类型。
However, it is worth noting that JLS-3.10.1 - Integer Literals says (in part) 但是,值得注意的是JLS-3.10.1 - Integer Literals (部分)
An integer literal is of type
long
if it is suffixed with an ASCII letterL
orl
(ell);如果整数文字后缀为ASCII字母
L
或l
(ell),则整数文字的长度为long
; otherwise it is of typeint
( §4.2.1 ).否则它的类型为
int
( §4.2.1 )。The suffix
L
is preferred, because the letterl
(ell) is often hard to distinguish from the digit1
(one).后缀
L
是优选的,因为字母l
(ell)通常难以与数字1
(一)区分开。
Yes: it's readability. 是的:它的可读性。
It's easy to mistake 400l
for four thousand and one when you first glance at it. 当你第一眼看到它时,很容易将
400l
误400l
是四千一。
I find it more likely to interpret it correctly as four hundred long with the upper case L. 我发现它更有可能正确地解释为四百长的大写L.
Just to add to the answers already here we can actually prove that both 400l
and 400L
are the primitive long
using the Local Variable Type Inference.只是为了补充已经在这里的答案,我们实际上可以证明
400l
和400L
都是使用局部变量类型推断的原始long
。
var num = 10L;
System.out.println(num instanceof Object);
Will cause a compiler error as primitive types cannot be used with instanceof
.将导致编译器错误,因为原始类型不能与
instanceof
一起使用。
This is not the case with either Long num = 1L
as it autoboxed or with var num = new Long(1)
.自动装箱时
Long num = 1L
或var num = new Long(1)
都不是这种情况。
Though the latter will cause a deprecation warning in versions older than Java 9.尽管后者会在早于 Java 9 的版本中导致弃用警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.