Java - 是否有任何理由使用此格式:(长)0;而不是这个:0L;?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22354821/
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
Java - Is There Any Reason to Use This Format: (long) 0; Instead of This One: 0L;?
提问by Original Username
I couldn't find any information about this when searching StackOverflow or Google, and I've got some coworkers who disagree with my preference for how to initialize a simple Long
variable.
在搜索 StackOverflow 或 Google 时,我找不到任何关于此的信息,而且我有一些同事不同意我对如何初始化简单Long
变量的偏好。
Is there any reason to use one of these formats over the other?
是否有任何理由使用其中一种格式而不是另一种格式?
Long foo = (long) 0; // First way of doing it
Long bar = 0L; // Second way of doing it
I'm most interested if anyone knows if there is an efficiency difference here.
如果有人知道这里是否存在效率差异,我最感兴趣。
The reason I prefer the second way is because you can specify values less than Integer.MIN_VALUE
and greater than Integer.MAX_VALUE
, whereas Eclipse would complain with something along the lines of "The literal 10000000000 of type int is out of range" if you used the first way here.
我更喜欢第二种方式的原因是因为您可以指定小于Integer.MIN_VALUE
和大于 的值Integer.MAX_VALUE
,而如果您在这里使用第一种方式,Eclipse 会抱怨类似“int 类型的文字 10000000000 超出范围”之类的内容。
采纳答案by Anton Bessonov
There is no difference(except you mentioned). Compiler is smart enough.If I compile following class:
没有区别(除了你提到的)。编译器足够聪明。如果我编译以下类:
public class Test {
public static void main(String[] args) {
Long foo = (long) 0;
Long bar = 0L;
}
}
And then decompile them:
然后反编译它们:
$ javap -c Test.class
$ javap -c Test.class
Compiled from "Test.java"
public class Test {
public Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: lconst_0
1: invokestatic #2 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
4: astore_1
5: lconst_0
6: invokestatic #2 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
9: astore_2
10: return
}
I don't see any difference. Use one which is look better for you or corresponds to the conventions.
我看不出有什么区别。使用更适合您或符合惯例的一种。
To verify decompiler I compile this two lines independent and then calculate checksums:
为了验证反编译器,我独立编译这两行,然后计算校验和:
$ sha1sum Test.class_L Test.class_long
292a93b6433b5a451afdb41bd957667c91eebf23 Test.class_L
292a93b6433b5a451afdb41bd957667c91eebf23 Test.class_long
回答by christopher
Long foo = (long) 0; // First way of doing it
This way will create an int
then castit to a long
.
这种方式将创建一个int
然后将其转换为long
.
Long bar = 0L; // Second way of doing it
This is a long
literal, so it will only create a long
.
这是一个long
文字,所以它只会创建一个long
.
I imagine the difference is negligible, but it would be quicker to create a long
than to create an int
then cast to long
.
我认为差异可以忽略不计,但创建 along
比创建int
然后转换为long
.
Edit
编辑
As you correctly said, the first way will only allow you to convert an int
to a long
, which means that in one line, you can only ever create a long
the size of an int
.. which is pointless and a waste of memory.
正如您所说的那样,第一种方法只允许您将 an 转换int
为 a long
,这意味着在一行中,您只能创建一个..long
的大小,int
这毫无意义且浪费内存。
回答by christopher
(long) 0
casts an int
to a long
. 0L
isa long
. So if you want to use a long
, use one.
(long) 0
将 an 转换int
为 a long
。0L
是一个long
。因此,如果您想使用long
,请使用一个。
回答by supercat
One difference which isn't mentioned is that the compiler will reject a numeric literal which is larger than 2147483647 but doesn't have an L
suffix. If there are semantic reasons why one needs a literal to be processed as type long
rather than int
, but the code would malfunction if given any value larger than 2147483647, casting an unsuffixed literal would retain the constraint on the value while forcing the type to be processed as long
. Such a thing might be appropriate, for example, if a value would be multiplied by 256 and then later divided by 256, and if the result of such division would need to fit in an int
.
未提及的一个区别是编译器将拒绝大于 2147483647 但没有L
后缀的数字文字。如果存在语义原因为什么需要将文字作为类型long
而不是来处理int
,但是如果给定任何大于 2147483647 的值,代码就会出现故障,则转换无后缀文字将保留对该值的约束,同时强制将类型处理为long
. 这样的事情可能是合适的,例如,如果一个值乘以 256 然后再除以 256,并且如果这种除法的结果需要适合int
.