As far as I know, type generics happens in the following code:
public class Test {
public void a(List<Object> list) {
}
public void a(List<String> list){
}
public void a(List<Integer> list){
}
}
when the compiler compiles the code, the generic types will be erased so all the signatures are exactly same, which looks as follows:
public class Test {
public void a(List list) {
}
public void a(List list){
}
public void a(List list){
}
}
If this is the logic, then:
List<Object> objList = new ArrayList<>();
List<String> strList = new ArrayList<>();
objList = strList;
is actually:
List objList = new ArrayList<>();
List strList = new ArrayList<>();
objList = strList;
which is OK because both of the two lists are same type.
However, In the above code, objList = strList
will result an error.
Not quite sure what is the real logic. And what is more, the difference between List<?>
, List(without any braces) and List< Object>