Generics class<t>, T, and class<?> in Java

Source: Internet
Author: User

A. Difference

The individual T represents a type, and class<t> represents the class corresponding to that type, class< > classes that represent types that are indeterminate

EElement (used in the collection because the element is stored in the collection) T-type (Java Class) K-Key (key) V-Value (values) N-number (numeric type)? -represents an example of an indeterminate Java type: 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 undefined, and the list is not specified<?>is the same. The role of generics:1, Generic: Java Code collection code list<T> list=NewArraylist<t>(); T T=list.get (0); 2, no generics: Java Code Collection code list List=NewArrayList (); T T= (T) list.get (0);

second, how to create an instance of the class<t> type?

Just like using non-generic code, there are two ways to call the method Class.forName () or use the class constant X.class. Class.forName () is defined to return class<?>. On the other hand, the class constant x.class is defined as having type class<x>, so String.class is the class<string> type.

Generics class<t>, T, and class<?> in Java

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.