Why doesn't the diamond operator work within a addAll() call in Java 7?

前端 未结 3 2023
南旧
南旧 2021-01-17 14:13

Given this example from the generics tutorial.

List list = new ArrayList<>();
list.add(\"A\");

// The following statement should fail si         


        
3条回答
  •  青春惊慌失措
    2021-01-17 15:12

    When compiling a method invocation, javac needs to know the type of the arguments first, before determining which method signature matches them. So the method parameter type isn't known before the argument type is known.

    Maybe this can be improved; as of today, the type of the argument is independent of the context.

提交回复
热议问题