[英]Why use Long.valueOf(…) rather than a long literal?
Recently I stumbled over code where people write stuff like 最近我偶然发现了人们写东西的代码
Long myLong = Long.valueOf(42);
// instead of
Long myLong = 42L;
I have no clue why one would do this, except for personal taste regarding readability. 我不知道为什么会这样做,除了有关可读性的个人品味。
Am I missing something? 我错过了什么吗?
with direct assignment you are required to cast if assigning int
to Long
( int
to primitive long
is implicit) and they get autoboxed
automatically using Long.valueOf
使用直接赋值,如果将
int
赋值为Long
( int
to primitive long
is implicit),则autoboxed
进行Long.valueOf
并使用Long.valueOf
自动进行自动Long.valueOf
Long myLong1 = Long.valueOf(42);
Long myLong2 = Long.valueOf(42L);
Long myLong3 = 42L;
Long myLong4 = (long) 42;
otherwise they are all same See bytecode output from javap
否则它们都是相同的从
javap
看到字节码输出
public static void main(java.lang.String[]);
Code:
0: ldc2_w #16 // long 42l
3: invokestatic #18 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
6: astore_1
7: ldc2_w #16 // long 42l
10: invokestatic #18 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
13: astore_2
14: ldc2_w #16 // long 42l
17: invokestatic #18 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
20: astore_3
21: ldc2_w #16 // long 42l
24: invokestatic #18 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
27: astore 4
29: return
However using new Long(42L)
should be avoided if not absolutely necessary and one of above statement needs to be used in favor of this as valueOf
methods normally cache a range of values ( FlyWeight Design Pattern
) by JVM internally 但是,如果不是绝对必要的话,应该避免使用
new Long(42L)
并且需要使用上述语句之一作为valueOf
方法通常在内部通过JVM缓存一系列值( FlyWeight Design Pattern
)
Trivia: In case of integers & Oracle JVM the range can be controlled using -XX:AutoBoxCacheMax=
琐事:如果是整数和Oracle JVM,可以使用
-XX:AutoBoxCacheMax=
控制范围-XX:AutoBoxCacheMax=
The snippet 片段
Long myLong = 42L;
is internally the same as 在内部是相同的
Long myLong = Long.valueOf(42);
The compiler will generate the same bytecode. 编译器将生成相同的字节码。
I also think it's a reminder of java before java5, where there was no autoboxing, and where 我也认为这是java5之前的java的提醒,其中没有自动装箱,以及在哪里
Long l = 42L;
could not be compiled. 无法编译。
它们是等价的,编译器将为两者构建相同的字节码
valueOf
takes a primitive long
. valueOf
需要一个原始的long
。 For literals, I'd agree that 42L
is better, but if you have an int
or long
variable, Long.valueOf
is a good way to get a Long
. 对于文字,我同意
42L
更好,但如果你有一个int
或long
变量, Long.valueOf
是获得Long
的好方法。 valueOf
also uses a cache of values from -128 to 127, which gives it a slight performance edge over new Long(long)
for common values. valueOf
还使用-128到127之间的缓存,这使得它在常见值的new Long(long)
具有轻微的性能优势。
People who don't realize you can do it the other way? 没有意识到你可以通过其他方式做到的人?
I have to wonder if the compiler is smart enough to convert the Long.valueOf to the same bytecode, for constant calls. 我不得不怀疑编译器是否足够聪明,可以将Long.valueOf转换为相同的字节码,以进行常量调用。 Otherwise there would be a tiny performance hit (not significant, but you might notice it heavily-run, tight loops).
否则会有一个微小的性能损失(不重要,但你可能会注意到它运行严重,紧密的循环)。
The valueOf version is useful for casting primitive values safely, handy if you're doing a bunch of casts between primitive types and don't want to (for example) cause problems doing: (int)longBiggerThanIntCanHandle valueOf版本对于安全地转换原始值非常有用,如果你在原始类型之间进行一系列强制转换并且不希望(例如)导致问题,那么它很方便:(int)longBiggerThanIntCanHandle
(You wouldn't care about doing (long)intValue, but if you're doing a bunch of conversions both ways, it's good to use valueOf as a convention, for safety.) (你不会关心做(长)intValue,但是如果你在两种方式都做了很多转换,那么为了安全起见,最好使用valueOf作为约定。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.