Type inference is based on callsite.
However, type inference only applies to return types if the return value is assigned to a variable. It is written in spec
If the result of a method arises in the context where it will undergo an assignment transformation, then [...]
Otherwise, any parameters of an unresolved type always become Object .
In your example, this will work because there is an overload of print(Object) .
On the other hand, look at this code:
print(get()); public void print(Boolean x) { } public <T> T get() { return (T) Boolean.FALSE; }
This will give a compilation error because the compiler prints get() as returning Object before looking at print() .
SLaks source share