By convention, T
is used to represent a type parameter , or type parameter , used in the declaration of a generic class.
As already mentioned in Jean's response, there is a convention for more common cases, although you can use any letter in practice.
A classic example is the declaration of class List
, which represents a list of elements. So we have:
public interface List<E> extends Collection<E> { ... }
At the time of using the list, we can then define the type E
of the list, like this:
List<String> nomes = new ArrayList<>();
And now the compiler will warn if we try to put anything other than String
in the list.
Only sometimes we want to explicitly leave a list without a defined type. This could only be done with List
, but in this case the compiler will think you forgot to declare the type.
For this we use the wildcard ?
, as in the example:
int contar(List<?> qualquerTipo) {
return qualquerTipo.size();
}
Note that I do not need to know the type to count the elements of the list, so it is a perfectly valid use.
There are other applications for this, but it is generally a type of generic meta-programming used in utilitarian frameworks or routines where the actual type does not matter.
This should not be used frequently in system implementations we do on a day-to-day basis, as it is much more insecure.