I wrote a simple program using generic type. However, the simple example behaves differently on different JDK versions.
Simple code is as follows:
import java.util.List;
public class GenericTypes {
public static String method(List<String> list) {
System.out.println("Method method(List<String> list) is invoked.");
return "";
}
public static int method(List<Integer> list) {
System.out.println("Method method(List<Integer> list) is invoked.");
return 0;
}
}
Scenario#1 ==> on JDK 1.7, Compile error appears
`Method method(List<String>) has the same erasure method(List<E>) as another method in type GenericTypes.`
Scenario#2 ==> on JDK 1.6 or 1.5, there is no Compile error.
Screenshot for the code and output in console is as follows:
As we know, Generic Type is introduced since JDK 1.5. However, with the above simple example, it behaves differently in different JDK versions.
Here is my questions:
Q1==> What change has been made in higher JDK version (like JDK 1.7) to make Generic Type behaviors differently in some scenarios, such as the above simple example?
Q2==> Is compile error better than no compile error in above example?
Please help me out with this. Thank you very much in advance.