Java 泛型 - 原始类型
如果在创建过程中未传递其类型参数,则原始类型是泛型类或接口的对象。 以下示例将展示上述概念。
示例
使用您选择的任何编辑器创建以下 java 程序。
GenericsTester.java
package com.tutorialspoint;
public class GenericsTester {
public static void main(String[] args) {
Box<Integer> box = new Box<Integer>();
box.set(Integer.valueOf(10));
System.out.printf("Integer Value :%d
", box.getData());
Box rawBox = new Box();
//No warning
rawBox = box;
System.out.printf("Integer Value :%d
", rawBox.getData());
//Warning for unchecked invocation to set(T)
rawBox.set(Integer.valueOf(10));
System.out.printf("Integer Value :%d
", rawBox.getData());
//Warning for unchecked conversion
box = rawBox;
System.out.printf("Integer Value :%d
", box.getData());
}
}
class Box<T> {
private T t;
public void set(T t) {
this.t = t;
}
public T getData() {
return t;
}
}
这将产生以下结果。
输出
Integer Value :10 Integer Value :10 Integer Value :10 Integer Value :10

