String
関数の戻り値の型の場合に、コンパイラが型パラメーターを正しく推測できる理由。
public class Generics {
private static List<String> function() {
return new ArrayList<>();
}
}
ただし、推論する型がメソッド パラメーターの場合は失敗します。
public class Generics {
public static void main(String[] args) {
method(new ArrayList<>());
}
private static void method(List<String> list) {
}
}
この場合のエラーは次のとおりです。
The method method(List<String>) in the type Generics is not applicable
for the arguments (ArrayList<Object>)