Java Foundation----Java e,t,? The difference

Source: Internet
Author: User

Encountered in <A>,<B>,<K,V> and so on, is used in Java generics.
General use of <T> to declare type holder names, when customizing generic classes, class holder names can use T (type)
If the element of the container can use E (element), if the key value match can use K (key) and V (Value), etc.
If <?>, then the default is to allow object and its sub-classes, which are all objects in Java.


So say, if it is the word every a,b,c,d ... The definition is generics, where T is just the meaning of the name. T---type,e----Element
K----key, V----value
What if it is? Defined is the normal object or its subclasses.

To illustrate:
Set<t> represents an instance of the T class in the collection.
List<e> represents an instance of class E in the collection.
List<?> indicates that the object type in the collection is indeterminate, unspecified
The List is the same as list<?>.

the role of generics:
1, with the generic type:

List<t> list=new arraylist<t> ();
T t=list.get (0);

2. No generics:

List list=new ArrayList ();
T t= (t) list.get (0);

Believe you have seen:
A, generics only determine the type of elements within the collection, but it is at compile time to determine the type of the element and then removed when the strong turn is no longer required,
Enhanced program readability, stability and efficiency
b, without generics, if the set operation is mounted, then the element is treated as object, losing its own type, and when it is removed from the collection,
Often need to be transformed, inefficient and prone to mistakes

Java Foundation----Java e,t,? The difference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.