Home  >  Q&A  >  body text

spring - Java源代码中泛型的提问?

**在查看JDK源代码的时候,常会发现如下几种类型:?、T、Object、K、V、E,众所周知,?代表的是不确定的Java类型,T代表实际使用之前就已经确定的Java类型,Ojbect是所有类的基类,在强制转换的时候,可以被频繁使用,K和V表示的是key,value,E代表的是Element,Element是元素的意思,表示它是作为一个集合的元素。但是源码中有的时候用的是E,有的时候用的是V,它们在表示单一的类型的时候有什么区别呢?
比如:**
1.Callable接口,为什么用的是V

  public interface Callable<V> {
        V call() throws Exception;
    }

2.List接口中为什么使用E

public interface List<E> { 

           void add(E x); 

           Iterator<E> iterator(); 

}

得出的结论:V、E、T代表的是一种类型,并没有实质的区分。就好像在数学中,变量x y z都表示变量名,在同一环境下,区分开来是三个不同的变量。

PHP中文网PHP中文网2741 days ago273

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 17:53:41

    Everything is the same, there is no difference. T, K, V, E are just the same name. The different names are just to tell us the meaning of this type of data.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:53:41

    ? is a keyword, which means it matches any type. There is no difference with other letters such as T, K, V, etc. You can just choose the letter you like.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:53:41

    It doesn’t matter what you use. If you really want to know, ask the author why he uses these letters.

    reply
    0
  • Cancelreply