See below simple snippet:
public class GenericsOverloadingDistinguish<T> { public void print1(T t) { System.out.println("t"); } public void print1(Integer i) { System.out.println("integer"); } } public static void main(String[] args) { new GenericsOverloadingDistinguish<Integer>().print1(new Integer(1)); } This would cause an ambiguous method call and will not compile.
This is utterly confusing on the user of that class. It is not able to call neither print1(T t) nor print1(Integer i) simple because it unfortunately used Integer as the generic type.
I understand generics is compile-time and there is type erasure, but doesn't Java have something to prevent such errors?
What if the GenericsOverloadingDistinguish Class is given and can't be changed, and I just need to invoke print1(T t) with T being an Integer?