<?> vs <T>

I came across a function that looked like this:

public void function(Class<?> clazz) {...} 

What are the advantages / disadvantages of changing the method:

 public <T> void function(Class<T> clazz) {...} 

edit: what is compile time / runtime.

+7
java generics
source share
4 answers

todd.run is absolutely right, but that is only half the answer. There are also use cases for <T> over <?> (Or vice versa) that apply when you do not add a type parameter to a class that includes this method. For example, consider the difference between

 public <E extends JLabel> boolean add(List<E> j) { boolean t = true; for (JLabel b : j) { if (b instanceof JLabel) { t = t && labels.add(b); } } return t; } 

and

 public boolean add(List<? extends JLabel> j) { boolean t = true; for (JLabel b : j) { if (b instanceof JLabel) { t = t && labels.add(b); } } return t; } 

The first method will not actually compile UNLESS if you add the appropriate parameter type to the class class, while the second method WILL compiles regardless of whether the wrapper class has a type parameter. If you do not use <?> , You are responsible for telling the compiler how to get the type to be filled with the letter used in its place. You often encounter this problem - do you need to use it? rather than T - when trying to write generic methods that use or require "extensions" and "super." A better but more thoughtful solution to this problem can be found on page 18 of the Gilad Bracha Generics Tutorial (PDF) . Also see this question , whose answer highlights these issues.

Check this link for information on your second question: Java generics - type wiping - when and what happens . Although I do not know the answer to your question about the difference in compilation time between <?> And <T> , I am sure that the answer can be found on this FAQ , which Erickson mentioned in this post.

+7
source share

Using "?" it is the same as any, whereas T means a certain type. So compare these interfaces:

 public interface StrictClass<T> { public T doFunction(Class<T> class); } public interface EasyClass<T> { public > doFunction(Class<?> class); } 

Now we can create classes:

 public class MyStrictClass implements StrictClass<String> { public String doFunction(Class<String> stringClass) { //do something here that returns String } } public class MyEasyClass implements EasyClass<String> { public String doFunction(Class<?> anyClass) { //do something here that returns String } } 

Hope this helps!

+10
source share

In principle, they are equivalent. You can use the first syntax where you do not need to declare anything like T.

UPDATE: oh and T can be used to combine types together: if Class<T> used in different parts of the function, it will refer to the same class, but not to Class<?> .

+2
source share

A good resource could be the following: http://sites.google.com/site/io/effective-java-reloaded The interesting part related to your question starts from the 5th minute. Just depending on what the previous users said.

Hope this helps:]

0
source share

All Articles