Difference between <? super T> and <? extends T> in Java

I love the answer from @Bert F but this is the way my brain sees it.

I have an X in my hand. If I want to write my X into a List, that List needs to be either a List of X or a List of things that my X can be upcast to as I write them in i.e. any superclass of X...

List<? super   X>

If I get a List and I want to read an X out of that List, that better be a List of X or a List of things that can be upcast to X as I read them out, i.e. anything that extends X

List<? extends X>

Hope this helps.

这两个是在编译器做类型擦除(自动帮我们转换类型)时起作用

extend是producer,用于获取,如果容器里都是某个类的子类的话,那么编译器可以安全的将其类型转换为基类或接口赋给某给基类或接口的引用

super是consumer,用于添加,如果容器是都是某个类的超类的话,那么编译器可以安全的把要放进去的这个类的子类转换为超类的类型后赋值给某个位置的元素

时间: 2024-10-09 17:56:38

Difference between <? super T> and <? extends T> in Java的相关文章

?super T 和? extends T区别

Java 泛型 关键字说明 ? 通配符类型 <? extends T> 表示类型的上界,表示参数化类型的可能是T 或是 T的子类 <? super T> 表示类型下界(Java Core中叫超类型限定),表示参数化类型是此类型的超类型(父类型),直至Object extends 示例 static class Food{} static class Fruit extends Food{} static class Apple extends Fruit{} static clas

Difference between Singleton Pattern vs Static Class in Java

Singleton pattern  vs  Static Class (a class, having all static methods) is another interesting questions, which I missed while blogging about Interview questions on Singleton pattern in Java. Since both Singleton pattern and static class provides go

java泛型中&lt;? super String&gt;和&lt;? extends String&gt; 的区别

(1)<? super String> is any class which is a superclass of String (including String itself). (In this case, the only other suitable class is Object.) 即包括String的父类和它本身的类. (2) <? extends String> (which in this specific case wouldn't be very usefu

泛型中? super T和? extends T的区别

来源:并发编程网 链接:http://ifeve.com/difference-between-super-t-and-extends-t-in-java/ 经常发现有List<? super T>.Set<? extends T>的声明,是什么意思呢?<? super T>表示包括T在内的任何T的父类,<? extends T>表示包括T在内的任何T的子类,下面我们详细分析一下两种通配符具体的区别. extends List<? extends Nu

java ? super E 和 ? extends E

/** * 泛型固定下边界 * ? super E * * 泛型固定上边界 * ? extends E */ public static void main(String[] args) { //demo1(); TreeSet<Student> ts1 = new TreeSet<>(new CompareByAge()); ts1.add(new Student("张三", 33)); ts1.add(new Student("李四",

java的几个特性

前言 本文主要介绍java语言的三个特性:类型协变和逆变,动态代理和静态代理,注解. 协变和逆变 借用Treant的博文,逆变与协变用来描述类型转换(type transformation)后的继承关系,其定义: 如果A.B表示类型,f(?)表示类型转换,≤表示继承关系(比如,A≤B表示A是由B派生出来的子类): f(?)是逆变(contravariant)的,当A≤B时,有f(B)≤f(A)成立: f(?)是协变(covariant)的,当A≤B时, 有f(A)≤f(B)成立: f(?)是不变

Java中的逆变与协变(转)

看下面一段代码 Number num = new Integer(1); ArrayList<Number> list = new ArrayList<Integer>(); //type mismatch List<? extends Number> list = new ArrayList<Number>(); list.add(new Integer(1)); //error list.add(new Float(1.2f)); //error 有人会

Java中的逆变与协变

看下面一段代码 Number num = new Integer(1); ArrayList<Number> list = new ArrayList<Integer>(); //type mismatch List<? extends Number> list = new ArrayList<Number>(); list.add(new Integer(1)); //error list.add(new Float(1.2f)); //error 有人会

Java 多线程之 Runnable VS Thread 以及资源共享问题

对于 Java 多线程编程中的 implements Runnable 与 extends Thread,部分同学可能会比较疑惑,它们之间究竟有啥区别和联系呢?他们是不是没啥区别随便选呢?实际中究竟该选择哪一个呢? 甚至网上不少博客文章以讹传讹得出不少谬论,那今天的走进科学栏目将带您一一揭开谜底. 1.区别: 其实这块主要是围绕着接口和抽象类的区别以及一些设计原则而言的. 1.1 Inheritance Option: The limitation with "extends Thread&qu