简体   繁体   English

Java:指定long时L和l(小写L)有区别吗?

[英]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 ?当我将一个数字指定为具有常量值400long时,使用400L400l之间有什么区别吗?

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的包装器Longl

No practical difference. 没有实际差别。 Either L or l can be used, both indicate a long primitive. 可以使用Ll ,两者都表示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 letter L or l (ell); 如果整数文字后缀为ASCII字母Ll (ell),则整数文字的长度为long ; otherwise it is of type int ( §4.2.1 ). 否则它的类型为int§4.2.1 )。

The suffix L is preferred, because the letter l (ell) is often hard to distinguish from the digit 1 (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. 当你第一眼看到它时,很容易将400l400l是四千一。

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.只是为了补充已经在这里的答案,我们实际上可以证明400l400L都是使用局部变量类型推断的原始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 = 1Lvar 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM