繁体   English   中英

在Java中长时间取消装箱

[英]Unboxing Long in java

在一些代码中,我看到了这一点:

private void compute(Long a, Long b, Long c) {
        long result = a-(b+c);
...

结果存储在原始long中而不是对应于其操作数的Long对象中,这似乎有点奇怪。

是否有任何理由将结果存储为原语?

结果存储在原始long中而不是对应于其操作数的Long对象中,这似乎有点奇怪。

不,“奇怪”是您可以在Long对象上使用+-运算符。 在Java 5之前,这可能是语法错误。 然后介绍了自动装箱/拆箱。 您在这段代码中看到的是自动拆箱 :运算符需要原语,因此编译器会自动在对象上插入对longValue()的调用。 然后对原始long值执行算术运算,结果也是long ,可以存储该long ,而无需在变量上进行进一步转换。

至于为什么代码要这样做,真正的问题是为什么有人会使用Long类型而不是long类型。 可能的原因:

  • 这些值来自提供Long值的某些库/ API。
  • 值存储在不能保存基元的集合( ListMap )中。
  • 松懈或货物崇拜程序设计
  • 需要具有null值的功能,例如,用信号通知不可用或未初始化的数据。

请注意, Long保留null值的能力意味着计算(或更具体地说,由编译器插入的longValue()调用)可能会因NullPointerException失败-代码应以某种方式处理这种可能性。

原因很明显:result被声明为原始。

算术运算符+和-不是为装箱类型(例如Long)定义的,而是为基本类型(例如long)定义的。

结果也很长。 请参阅自动装箱和拆箱教程

将其自动装箱成Long可能会降低性能成本。 这也是不必要的,因为

  1. 我们知道它将为非null(如果a,b或c为null,则将发生NullPointerException)。
  2. 如果稍后在需要Long的地方使用它,它将被隐式自动装箱。

根据您的需要。

Autoboxing unboxingunboxing可以在需要对象和原始类型可用的任何地方进行

通常,您应该更喜欢使用基元,尤其是当您确定基元不能为null时。 如果您坚持使用盒装类型,请务必加倍思考当它为null时会发生什么。 Java将自动为您进行装箱和拆箱,但是盯着一个int并想知道为什么会出现NullPointerException可能很有趣。

从Java 1.5开始,自动装箱和拆箱会在需要时隐式发生。

下一行:

long result = a-(b+c);

...要求Java使用3 Long来获取表达式的结果,然后将其存储在原始long中。 在Java 5之前,它会抱怨类型不匹配-但是如今,它仅假设您的意思,并自动为您完成从对象到原始类型的转换。

但是,在此示例中,除非这里没有给出其他一些充分的理由,否则绝对没有理由首先将参数作为盒装对象类型。

根据javadoc

Boxing conversion converts expressions of primitive 
type to corresponding expressions of reference type. 
Specifically, the following nine conversions are called the boxing conversions:

From type boolean to type Boolean

From type byte to type Byte

From type short to type Short

From type char to type Character

From type int to type Integer

From type long to type Long

From type float to type Float

From type double to type Double

From the null type to the null type


Ideally, boxing a given primitive value p, would always yield an identical reference.     
In practice, this may not be feasible using existing implementation techniques. The  
rules above are a pragmatic compromise. The final clause above requires that certain 
common values always be boxed into indistinguishable objects. The implementation may  
cache these, lazily or eagerly. For other values, this formulation disallows any 
assumptions about the identity of the boxed values on the programmer's part. This would 
allow (but not require) sharing of some or all of these references.

This ensures that in most common cases, the behavior will be the desired one, without     
imposing an undue performance penalty, especially on small devices. Less memory-limited 
implementations might, for example, cache all char and short values, as well as int and 
long values in the range of -32K to +32K.`

这是Oracle Doc源

您的疑问的答案是

  1. Java中的自动装箱和自动拆箱,分别从原始类转换为包装类对象,反之亦然。
  2. 自动装箱意味着内部编译器使用原始类的valueOf()方法;自动拆箱意味着内部编译器使用xxxValue()方法。
  3. 假设有私有无效计算(Long a,Long b,Long c){long result = a-(b + c);

它使此转换为a.longValue()-(b.longValue()+ c.longValue()),这意味着即使在您的语句执行加法运算之前,编译器也会将long类型的原语提供为操作数的输入。因为JAVA是静态和强类型语言。

因此,您得到长型输出

我希望我消除了你的疑虑

暂无
暂无

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

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