原始类型是泛型类或接口的对象,如果其类型参数在创建过程中传递。以下示例将展示上述概念。
使用您喜欢的编辑器创建以下java程序,并保存到文件:RawTypes.java 中,代码如下所示 -
package com.zyiz; public class RawTypes { public static void main(String[] args) { Box5<Integer> box = new Box5<Integer>(); box.set(Integer.valueOf(105)); System.out.printf("Integer Value :%d\n", box.getData()); Box5 rawBox = new Box5(); // No warning rawBox = box; System.out.printf("Integer Value :%d\n", rawBox.getData()); // Warning for unchecked invocation to set(T) rawBox.set(Integer.valueOf(106)); System.out.printf("Integer Value :%d\n", rawBox.getData()); // Warning for unchecked conversion box = rawBox; System.out.printf("Integer Value :%d\n", box.getData()); } } class Box5<T> { private T t; public void set(T t) { this.t = t; } public T getData() { return t; } }
执行上面代码,得到以下结果 -
Integer Value :105 Integer Value :105 Integer Value :10 Integer Value :10