Yan*_*euc 25 java generics type-erasure generic-method
我正在研究Java通用功能,我不知道如何解释以下main
方法中的第三行:
public class Example4 {
public static void main(final String[] args) {
System.out.println(Util.<String>compare("a", "b"));
System.out.println(Util.<String>compare(new String(""), new Long(1)));
System.out.println(Util.compare(new String(""), new Long(1)));
}
}
class Util {
public static <T> boolean compare(T t1, T t2) {
return t1.equals(t2);
}
}
Run Code Online (Sandbox Code Playgroud)
第一行编译,运行和返回(如预期的那样)false
.
第二行没有按预期编译,因为我明确地混合String
和Long
.
第三行编译,运行并返回false但我不确定它是如何工作的:编译器/ JVM是否将参数类型实例T
化为Object
?(另外,有没有办法获得这个声明类型的T
运行时?)
谢谢.
Aza*_*Aza 20
共享继承类型String
和Long
是Object
.
当您运行此函数时,Util.<String>compare(
编译器期望找到两个字符串输入,并在没有时发出错误.但是,运行它而不会<String>
导致使用最接近的共享继承类型 - 在这种情况下,Object
.
因此,当compare
接受t1
并且t2
,它们已经被转换为Object
,并且代码运行良好.
要在运行时获取实际类型,您可以使用与任何其他对象相同的技术:getClass()
从Object
类继承的技术.
答案似乎超出了@Telthien和@newacct的答案.我很想知道自己"看到"之间的区别:
System.out.println(Util.<String>compare("a", "b"));
Run Code Online (Sandbox Code Playgroud)
明确键入,并:
System.out.println(Util.compare(new String(""), new Long(1)));
Run Code Online (Sandbox Code Playgroud)
隐式输入.
我使用前两行的变体进行了几次实验.这些实验表明,如果没有使用匿名/本地类技巧,编译器会在编译期间检查类型,但生成的字节码仅引用Object
,即使在第一行的情况下也是如此.
下面的代码表明,Object
即使在explicity类型参数的情况下,也可以安全地执行类型转换<String>
.
public final class Example44 {
public static void main(final String[] args) {
System.out.println(new Util44<String>().compare("a", "b"));
System.out.println(new Util44().compare(new String(""), new Long(1)));
}
}
final class Util44<T> {
private T aT;
public boolean compare(T t1, T t2) {
System.out.println(this.aT);
// I was expecting the second and third assignments to fail
// with the first invocation because T is explicitly a String
// and then to work with the second invocation because I use
// a raw type and the compiler must infer a common type for T.
// Actually, all these assignments succeed with both invocation.
this.aT = (T) new String("z");
this.aT = (T) new Long(0);
this.aT = (T) new Object();
return t1.equals(t2);
}
}
Run Code Online (Sandbox Code Playgroud)
该main
方法的字节码如下:
// Method descriptor #15 ([Ljava/lang/String;)V
// Stack: 7, Locals: 1
public static void main(java.lang.String[] args);
0 getstatic java.lang.System.out : java.io.PrintStream [16]
3 new ca.polymtl.ptidej.generics.java.Util44 [22]
6 dup
7 invokespecial ca.polymtl.ptidej.generics.java.Util44() [24]
10 ldc <String "a"> [25]
12 ldc <String "b"> [27]
14 invokevirtual ca.polymtl.ptidej.generics.java.Util44.compare(java.lang.Object, java.lang.Object) : boolean [29]
17 invokevirtual java.io.PrintStream.println(boolean) : void [33]
20 getstatic java.lang.System.out : java.io.PrintStream [16]
23 new ca.polymtl.ptidej.generics.java.Util44 [22]
26 dup
27 invokespecial ca.polymtl.ptidej.generics.java.Util44() [24]
30 new java.lang.String [39]
33 dup
34 ldc <String ""> [41]
36 invokespecial java.lang.String(java.lang.String) [43]
39 new java.lang.Long [46]
42 dup
43 lconst_1
44 invokespecial java.lang.Long(long) [48]
47 invokevirtual ca.polymtl.ptidej.generics.java.Util44.compare(java.lang.Object, java.lang.Object) : boolean [29]
50 invokevirtual java.io.PrintStream.println(boolean) : void [33]
53 return
Line numbers:
[pc: 0, line: 24]
[pc: 20, line: 25]
[pc: 53, line: 26]
Local variable table:
[pc: 0, pc: 54] local: args index: 0 type: java.lang.String[]
Run Code Online (Sandbox Code Playgroud)
实际上,所有调用总是对具有Object
正式参数类型的方法有意义,如另一个问题/答案中所解释的那样.为了得出结论,编译器总是使用Object
生成的字节码,无论是否存在明确类型参数(第一行)或隐式类型参数,但对象可以具有不同的公共超类Object
.