I investigate generic behaviour
I noticed that:
public class Hohol1 {
public class My<T> {
public <E> void test(Collection<E> es) { System.out.println("Collection<E>");
}
public void test(List<Integer> integerList) {
System.out.println("List<Integer>");
for (Integer integer : integerList) {
System.out.println(integer);
}
}
}
public static void main(String[] args) {
My my1 = new Hohol1().new My();
my1.test(new ArrayList<String>() { {add("1");} });
}
}
code above returns
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at GenericsTest.Hohol1$My.test(Hohol1.java:22)
at GenericsTest.Hohol1.main(Hohol1.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
But if I make My
non-generic so
public class Hohol1 {
public class My/*deleted generic type T*/ {
....
}
}
this code returns Collection<E>
It is surpised behaviour for me and I don't understand why it created so.
What Do you think about it?
P.S. I use double brace initialization for laconic