無引数のメソッドとGenericsの謎
public class Test { public static void main(String[] args) { String s; s = Test.<String>foo(); // (a) s = foo(); // (b) bar(Test.<String>foo()); // (c) bar(foo()); // (d) } static <T> T foo() { return null; } static void bar(String s) { } }
(a)(b)(c)はコンパイルが通り、(d)だけコンパイルエラーになる。
The method bar(String) in the type Test is not applicable for the arguments (Object)
直感的には(b)(d)がコンパイルエラーになるように思われるので、不思議な結果。(b)がエラーにならないのはどうしてだろう?