What is the difference between <?> and <T> in Java generics?

Source: Internet
Author: User

public static void Printcoll (Arraylist<?> al) {
Iterator<?> it = Al.iterator ();
while (It.hasnext ())
{
System.out.println (It.next (). toString ());
}
? and T both represent the type of uncertainty, but if it is T, then the function can operate on t, such as while it can be written like this
T t = It.next ();
System.out.println (t);

T custom generics and? Wildcard generics
1. There is only one place in the entire class that uses generics, and when used, it is noted that the parameter cannot invoke methods related to parameter types such as "+", such as printing out all the contents of any parameterized type set, and is suitable for wildcard generics <?>
public static void Printcollecton (Collection <?> Collection)
{
for (Object obj:collection)
{
System.out.println (obj);
}
}
2. When a type face is used to express the relationship between the two parameters or the parameter and the return value, that is, the uniform types of variables are used in two places of the method signature, or the type variables are used in the method body code, not only when the signature is used, which should be used with custom generics <T>. A generic party can invoke methods of some time type. For example, the Add method of a collection.
public static <T> t Autoconverttype (t obj)
{
     return (t) obj;
}

Three types of generics:
[1] Arraylist<t> al=new arraylist<t> (); Specifies that the collection element can only be of type T
[2] Arraylist<?> al=new arraylist<?> (); A collection element can be any type, which is meaningless, usually in a method, just to illustrate usage
[3] arraylist<? Extends e> al=new arraylist<? Extends E> ();
Limitations of generics:
? Extends e: Receives subtype of e type or E.
? Super E: Receives the type E or the parent of E.

The above describes the Java generics and what is the difference? , including the aspects of the content, want to be interested in other programming tutorials friends helpful.

What is the difference between <?> and <T> in Java generics?

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.