Is this type inference in java?

I have a general method

public <K extends Number> K get()
{
    ...
}

When I call this method, I use a syntax like:

Integer i = instance.<Integer>get();

However, this is also legal:

Integer i = instance.get();

My question is, is the second method invoking a type inference form in Java?

+3
source share
1 answer

Yes, this is type inference based on assignment type. It is listed in section 15.12.2.8 of the JLS:

If any of the arguments of the method type has not been inferred from the types of the actual arguments, they are now inferred as follows.

  • , (. 5.2) S, R - , R '= R [T1 = B (T1)... Tn = B (Tn)], B (Ti) , Ti , Ti, .

, :

  • S → R ', R ;
  • Bi [T1 = B (T1)... Tn = B (Tn)] → Ti, Bi - Ti,

(§15.12.2.7). , Ti <: Uk Ti glb (U1,..., Uk) (§ 5.1.1).

+5

All Articles