Why is Java's type inference so weak?

后端 未结 3 2099
遇见更好的自我
遇见更好的自我 2021-01-05 21:27

Say, I have a method:

public static  Collection addToCollection(T element, Collection collection) {
    collection.add(element);
          


        
3条回答
  •  伪装坚强ぢ
    2021-01-05 22:01

    The type inference system has been improved in Java 8, with the introduction of target typing, in order to give more expressivity to streams and lambdas. As a consequence your code compiles with Java 8.

    More about it in the updated tutorial, with a very similar example towards the very bottom of the page.

提交回复
热议问题