Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ? (wildcard) and Type Parameter in Java

Tags:

java

generics

Can somebody explain me what the difference is between these two methods? Are they same? They do look same to me in terms of what they solve. If they are same, why need ??

Method #1, Unbounded

public static void printList(List<?> list) {     for (Object elem : list)         System.out.println(elem + " ");     System.out.println(); } 

Method #2, Unbounded:

public static <T> void printList(List<T> list) {     for (Object elem : list)         System.out.println(elem + " ");     System.out.println(); } 

Method #1, Bounded

public static void printList(List<? extends Number> list) {     for (Object elem : list)         System.out.println(elem + " ");     System.out.println(); } 

Method #2, Bounded:

public static <T extends Number> void printList(List<T> list) {     for (Object elem : list)         System.out.println(elem + " ");     System.out.println(); } 
like image 386
Tarik Avatar asked Aug 14 '13 20:08

Tarik


People also ask

What is the difference between a wildcard bound and a type parameter bound?

What is the difference between a wildcard bound and a type parameter bound? A wildcard can have only one bound, while a type parameter can have several bounds. A wildcard can have a lower or an upper bound, while there is no such thing as a lower bound for a type parameter.

What is type parameter in Java?

A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

What is wildcard types in Java?

Wildcards in Java are basically the question marks which we use in generic programming, it basically represents the unknown type. We use Java Wildcard widely in situations such as in a type of parameter, local variable, or field and also as a return type.

What is a wildcard parameter?

In the Java programming language, the wildcard ? is a special kind of type argument that controls the type safety of the use of generic (parameterized) types. It can be used in variable declarations and instantiations as well as in method definitions, but not in the definition of a generic type.


1 Answers

They are the same in that they accept the same parameter types.

However, identifying the type with T (or whatever) lets you refer to the type elsewhere.

Edit: Examples:

Your unbounded examples do not make full use of the capabilities of parameterized types. You have:

public static <T> void printList(List<T> list) {     for (Object elem : list)         System.out.println(elem + " ");     System.out.println(); } 

And that's sufficient for that example of printing string representations, but consider this (very contrived, and no error handling):

public static <T> T getSecondItem (List<T> list) {     T item = list.get(1);     return item; } 

The return type is T, which allows you to safely do things like this, with compile time type-checking:

class MyClass {     public void myMethod () { } }  void somewhere () {     List<MyClass> items = ...;     getSecondItem(items).myMethod(); } 

A named type also lets you share the same type constraint in multiple places, e.g.:

public <T> int compareLists (List<T> a, List<T> b) {     ... } 

If you did not name the type, you could not specify the constraint that a and b are the same list type (you could use List<? extends T> for more flexibility).

You also asked "Why do I need ??". The real answer is: You don't. It's mostly for aesthetics, I suppose. Java strives to be a precise and clutter-free language. There are many situations where you simply don't care what type you are referring to. In those cases, you may use ? without cluttering code with unused type parameter declarations.

like image 61
Jason C Avatar answered Sep 21 '22 21:09

Jason C