gpt4 book ai didi

java - 混淆自动装箱与泛型的使用

转载 作者:行者123 更新时间:2023-12-01 10:29:34 26 4
gpt4 key购买 nike

据我所知,整数示例自动装箱用法是:

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无效,因为 88int这与 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

关于java - 混淆自动装箱与泛型的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35159350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com