Why doesn't it overload?

I have two methods:

public void add(List<String> strs) {...}

public void add(List<Integer> ints) {...}

I get compilation errors, so my question is why is this not an overload, and what is the advantage of using generics?

This is the error I get:

Method add(List<Integer>) has the same erasure add(List<E>) as another method in type Child
+4
source share
5 answers

Java Generics is a compilation-only language feature; common types (such as Stringand Integerhere) is erased at compile time, and the byte-code includes only the raw type, for example:

public void add(List strs) {...}

- , . , varargs, , .

, , List<String>, List String, .

+16

. , public void add(List<String> strs) public void add(List<Integer> ints){...........} , , .

+4

- , , ( , T), , , j ava.lang.Object - . a List<Integer> a List<String> ; .

, .

+2

Java () : List.

 public void add(List strs){

 }

List , .

0

, JVM , . /, , . Java , " ...".

0

All Articles