2013-05-31 41 views
2

在一些代碼,我看到這一點:拆箱龍在Java

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

這似乎有點奇怪的是,結果被存儲在一個原始,而不是對應的操作數a 對象。

是否有任何理由將結果存儲爲原語?

+3

更嚴重的問題是爲什麼'長'已被使用。它效率低得多。 –

+0

它以原始形式存儲,因爲您已將其聲明爲原語。 – enkor

+0

也許你可以在這裏找到一些線索[鏈接](http://stackoverflow.com/questions/239560/when-should-i-use-primitives-instead-of-wrapping-objects) – PbxMan

回答

0

根據您的需要,我的意思是溺愛。其中一個對象,預計

Autoboxingunboxing可以在任何地方發生,基本類型可

1

原因是顯而易見的:結果被聲明爲原始。

+1

這實際上不是原因。 –

+0

我不明白爲什麼這不是原因。如果在「結果」下你理解所聲明的變量,那麼這就是原因。如果在「結果」下你理解了操作「a-(b + c)」的結果,那麼原因是添加操作是爲基元定義的,這就是爲什麼長鍵在被添加之前自動拆箱。 – nakosspy

0

通常你應該更喜歡使用原語,特別是如果你確定它們不能爲null。如果你堅持使用盒裝類型,總是會特別想當它爲空時會發生什麼。 Java會自動爲你做拳擊和拆箱,但是盯着一個int並且想知道爲什麼你得到一個NullPointerException可能很有趣。

0

從Java 1.5開始,自動裝箱和取消裝箱會在需要時隱式發生。

0

下面的行:

long result = a-(b+c); 

...要求渣取使用3個Long S中的表達式的結果,然後將其存儲在原始長。在Java 5之前,它會抱怨類型不匹配 - 但現在它只是假定你的意思是你說的並且自動爲你完成從對象到原始類型的轉換。

但是在這個例子中,除非在這裏沒有提出一些其他的理由,否則絕對沒有必要將參數作爲盒裝的對象類型放在第一位。

3

這似乎有點奇怪,結果存儲在一個原始的long而不是對應於它的操作數的Long對象中。

沒有,什麼是「奇怪」的是,你可以使用+-運營長對象。在Java 5之前,這將是一個語法錯誤。然後引入自動裝箱/拆箱。你在這段代碼中看到的是autounboxing:操作員需要原始資料,所以編譯器會自動在對象上插入對longValue()的調用。然後對原始值long執行算術運算,結果也是long,可以將其存儲在變量上而無需進一步轉換。

至於爲什麼代碼做到這一點,真正的問題是,爲什麼有人會使用Long類型,而不是long。可能的原因:

  • 這些值來自某個提供Long值的庫/ API。
  • 這些值存儲在集合中(ListMap),該集合不能保存原語。
  • Sl or或cargo cult programming
  • 需要具有null值的能力,例如,以指示不可用或未初始化的數據。

注意的Long舉行null值的能力意味着計算(或者更具體地說,由編譯器插入longValue()電話)可能失敗,NullPointerException - 可能的代碼應處理莫名其妙。

0

作爲每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.` 

Here is the Oracle Doc source

0

算術運算符+和 - 不盒裝類型(例如長)定義,但對原語類型(例如長)。

結果也很長。請參閱Autoboxing and Unboxing tutorial

將其自動裝箱到Long中會導致較低的性能成本。這也是不必要的,因爲

  1. 我們知道這將是非空(如果A,B或C都爲空的,會出現一個NullPointerException)。
  2. 如果我們稍後在需要Long的地方使用它,它會被隱式地自動裝箱。