簡體   English   中英

泛型混淆自動裝箱用法

[英]confusion autoboxing usage with generics

據我了解,以Integer為例,自動裝箱用法是:

Integer iOb2 = 88; // auto-boxing
Integer iOb = new Integer(88) // is it auto-boxing ? I think no
                              // if it is auto-boxing what about above line?

上面的代碼段有效。 但是,您能否回答第二行是否自動裝箱? 使用泛型,我無法獲得預期的結果。

// A very simple generic class. 
// Here, T is a type parameter that
// will be replaced by a real type
// when an object of type Gen is created.
class Gen<T> {
  T ob; // declare an object of type T

  // Pass the constructor a reference to 
  // an object of type T.
  Gen(T o) {
    ob = o;
  }

  // Return ob, which is of type T.
  T getob() {
    return ob;
  }
}

// Demonstrate the generic class.
class HelloWorld {
  public static void main(String args[]) {
    // Create a Gen reference for Integers. 
    Gen<Integer> iOb; 
    Integer iOb2;
    // Create a Gen<Integer> object and assign its
    // reference to iOb.  Notice the use of autoboxing 
    // to encapsulate the value 88 within an Integer object.
    //iOb = 88; //error
    iOb2 = 88;

    // Get the value in iOb. Notice that
    // no cast is needed.  The type is already known.
    //int v = iOb.getob();
    System.out.println("value: " + iOb2);

    System.out.println();

    // Create a Gen object for Strings.
    Gen<String> strOb = new Gen<String>("Generics Test");

    // Get the value of strOb. Again, notice
    // that no cast is needed.
    String str = strOb.getob();
    System.out.println("value: " + str);
  }
}

對於此通用代碼,為什么整數值未引用為Gen<Integer>類型包裝器類型? 應當的。 不是嗎

編譯器將Integer iOb2 = 88實現為Integer iOb2 = Integer.valueOf(88) 那就是自動裝箱。

Integer iOb = new Integer(88)就是您構造一個Integer對象。 不自動裝箱。

自動裝箱僅用於將原始類型自動轉換為其等效的Object版本,例如,將intInteger 所有自動裝箱操作都是使用valueOf()方法完成的,此方法是為此特定目的在Java 5中添加的(已存在該方法的Boolean除外)。

因此, iOb = 88無效,因為88是一個int且與Gen<Integer>分配不兼容。

如果您編寫了iOb = new Gen<Integer>(88) ,那么您將在創建對象之前引起自動裝箱,因為構造函數需要一個Integer但您要提供一個int

證明

為了證明自動裝箱使用valueOf() ,我創建了以下代碼:

Boolean   a = true;
Character b = '1';
Byte      c = 1;
Short     d = 1;
Integer   e = 1;
Long      f = 1L;
Float     g = 1f;
Double    h = 1d;

與產生的javap -c命令反匯編(為清楚起見添加了空行):

 0: iconst_1
 1: invokestatic  #19                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
 4: astore_1

 5: bipush        49
 7: invokestatic  #25                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
10: astore_2

11: iconst_1
12: invokestatic  #30                 // Method java/lang/Byte.valueOf:(B)Ljava/lang/Byte;
15: astore_3

16: iconst_1
17: invokestatic  #35                 // Method java/lang/Short.valueOf:(S)Ljava/lang/Short;
20: astore        4

22: iconst_1
23: invokestatic  #40                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
26: astore        5

28: lconst_1
29: invokestatic  #45                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
32: astore        6

34: fconst_1
35: invokestatic  #50                 // Method java/lang/Float.valueOf:(F)Ljava/lang/Float;
38: astore        7

40: dconst_1
41: invokestatic  #55                 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
44: astore        8

new Integer(88)不是自動裝箱-最接近的可用術語是“裝箱”,但這實際上只是構造。

在第二個代碼示例中,您似乎正在嘗試將int自動裝箱到Gen對象中–這不是自動裝箱的方式。

自動裝箱僅適用於用該語言定義的特定類型-例如, int 只能自動裝箱為Integer ,而不能自動裝箱為您定義的類型(例如Gen

Integer iOb = new Integer(88); 不是自動裝箱, Integer在其構造函數重載之一中采用int參數(請參閱API )。

iOb2 = 88分配使用自動裝箱,因為它會將int常量分配給Integer引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM