以下代码
public class GenericsTest2 {
public static void main(String[] args) throws Exception {
Integer i = readObject(args[0]);
System.out.println(i);
}
public static <T> T readObject(String file) throws Exception {
return readObject(new ObjectInputStream(new FileInputStream(file)));
// closing the stream in finally removed to get a small example
}
@SuppressWarnings("unchecked")
public static <T> T readObject(ObjectInputStream stream) throws Exception {
return (T)stream.readObject();
}
}
在eclipse中进行编译,但不能在javac中进行编译(无法确定T的类型参数;对于具有上限T,java.lang.Object的类型变量T,不存在唯一的最大实例)。
当我将readObject(String file)更改为
@SuppressWarnings("unchecked")
public static <T> T readObject(String file) throws Exception {
return (T)readObject(new ObjectInputStream(new FileInputStream(file)));
}
它可以在eclipse和javac中进行编译。谁是正确的,eclipse编译器还是javac?