Say, I have a method:
public static Collection addToCollection(T element, Collection collection) {
collection.add(element);
Collections.emptyList()
returns a List<Object>
which is passed through your addToCollection()
method. And since Object is not Integer, this fails.
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.
Whats harm in doing this?
Integer i = 42;
List<Integer> emptyList = Collections.emptyList();
Collection<Integer> result = addToCollection(i, emptyList);
In java 8 it will be taken care.