简体   繁体   English

Java - 有没有理由使用这种格式:(长)0;而不是这一个:0L;?

[英]Java - Is There Any Reason to Use This Format: (long) 0; Instead of This One: 0L;?

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超出范围”第一种方式。

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
Long foo = (long) 0; // First way of doing it

This way will create an int then cast it 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 . 我认为差异可以忽略不计,但是创建一个long比创建一个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. 正如你所说正确的,第一种方式只会让你的转换intlong ,这意味着在一个行,你永远只能创建一个long的大小int ..这是没有意义的,浪费内存。

(long) 0 casts an int to a long . (long) 0int转换为long 0L is a long . 0L long So if you want to use a long , use one. 因此,如果你想使用long ,请使用一个。

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. 未提及的一个区别是编译器将拒绝大于2147483647但没有L后缀的数字文字。 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 . 如果存在语义原因,为什么需要将文字作为long类型而不是int类型处理,但如果给定任何大于2147483647的值,则代码会出现故障,在强制类型强制时,强制使用非语法文字将保留对值的约束处理了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 . 这样的事情可能是合适的,例如,如果一个值乘以256然后再除以256,并且如果这种除法的结果需要适合int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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