在Java中取消装箱长

在某些代码中我看到了这个:

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

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

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

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

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

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

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

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

原因很明显:结果被声明为原始的。

根据你的需要。我的意思是decelaration。

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

通常你应该更喜欢使用原语,特别是如果你确定它们不能为空。 如果你坚持使用盒装类型,总是要考虑当它为空时会发生什么。 Java会自动为你做装箱和拆箱,但是盯着一个int并想知道你为什么得到NullPointerException会很有趣。

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

以下行:

 long result = a-(b+c); 

…要求Java使用3 Long s获取表达式的结果,然后将其存储在原始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源代码

算术运算符+和 – 不是为盒装类型(例如Long)定义的,而是针对原始类型(例如long)定义的。

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

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

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