I am trying to learn Java Generics, and found the following code.
public static <T> void print(T a, T b){
System.out.println(a);
System.out.println(b);
}
public static void main(String[] args){
print(new ArrayList<String>(), 1);
}
Which works with no problem.
However when I change print
method to the following, it gives me compiling errors.
public static <T> void print(List<T> a, T b){
System.out.println(a);
System.out.println(b);
}
Error:
GenericTest.java:9: error: method print in class GenericTest cannot be applied to given types;
print(new ArrayList<String>(), 1);
^
required: List<T>,T
found: ArrayList<String>,int
reason: no instance(s) of type variable(s) T exist so that argument type int conforms to formal parameter type T
where T is a type-variable:
T extends Object declared in method <T>print(List<T>,T)
1 error
Can anyone help me understand the errors?