What is the difference between wildcards and type variables?

Consider the following Employee class and a subclass called Manager-

public class Employee { private String name; public Employee(String name) { this.name = name; } public String getInfo() { return name; } } public class Manager extends Employee { public Manager(String name) { super(name); } } 

In another class, I defined two functions as follows:

 import java.util.ArrayList; public class WildCardsAndTypeVariables { public static <T extends Employee> void displayInfo(ArrayList<T> employees) { for (int i=0; i<employees.size(); i++) { Employee employee = (Employee) employees.get(i); System.out.println(employee.getInfo()); } } public static void displayInfo2(ArrayList<? extends Employee> employees) { for (int i=0; i<employees.size(); i++) { Employee employee = (Employee) employees.get(i); System.out.println(employee.getInfo()); } } public static void main(String[] args) { Employee e1 = new Employee("John"); Employee e2 = new Employee("Joe"); Employee e3 = new Manager("Joseph"); ArrayList<Employee> employees = new ArrayList<Employee>(); employees.add(e1); employees.add(e2); employees.add(e3); displayInfo(employees); displayInfo2(employees); } } 

I get the same output from displayInfo () and displayInfo2 ().

So, looking at this example, what is the difference between wildcards and type variables?

+4
source share
3 answers

In this case, when the method accepts only one parameter and has a return type of void type, you do not get any benefit or really no difference in semantics between the version of the type of the wildcard type and the version of the general method. The real power of common methods arises when you use a type variable more than once in a method signature (parameter types or return type), a trivial example:

 public static <T extends Employee> T firstEmployee(ArrayList<T> employees) { return employees.get(0); } 

That says: "This method accepts an ArrayList whose members are all instances of a subclass of Employee (or Employee ), and the return value is an instance of the same class." This is something you cannot express using only wildcards.

+3
source

When you use a type (example T), you can send ONLY a specific type of Concrete object.

But when you use wild cards, you define the boundaries of objects. Thus, you can transfer objects of type T (or) of any object of type T.

In your case, you only have Empoylee, so there is not much difference. But let's say you have a Contractor class that extends Employee. Then you will see the difference.

+1
source

There are many problems in your example.

In displayInfo2 should you use ? extends Employee ? extends Employee , as you read, not write, Employee objects in the passed array. For example, if you want to pass the Manager array to this method, it will not check the type until you make this correction.

Once this is fixed, you do not need to call employees.get() in any of the methods.

The difference is simply that you gave the unknown type a name ( T ) in displayInfo . Some situations in Java require that you have a name for the type (I will continue to edit this post with links), but in this particular example you are fine with just a template, since all you care about is the Employee part.

0
source

All Articles