泛型只在编译期有效,运行期无效
@org.junit.Test
public void demo01() {
ArrayList<String> list = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
System.out.println(list.equals(list2));
}
泛型类
public class Generic<T> {
private T type;
public Generic(T type) {
this.type = type;
}
public T getType() {
return type;
}
public void setType(T type) {
this.type = type;
}
}
泛型方法
public <V> V getGeneric(V v) {
System.out.println(v);
return v;
}
泛型通配符
public class Generic<T> {
public void a(List<T> list) {
System.out.println(list);
}
public void b(List<?> list) {
System.out.println(list);
}
public static void main(String[] args) {
Generic<String> generic = new Generic<>();
generic.a(new ArrayList<>());
generic.b(new ArrayList<Integer>());
}
}