---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 8 Edition"
java_version: 8
chapter: "Chapter 18. Type Inference"
section: "18.5.4"
heading: "18.5.4. More Specific Method Inference"
parent: "18.5. Uses of Inference"
source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-18.html#jls-18.5.4
id: "18.5.4"
url: https://jvm.obfuscated.software/jls/se8/18.5.4.html
raw: https://jvm.obfuscated.software/jls/se8/18.5.4.md
---

# 18.5.4. More Specific Method Inference

When testing that one applicable method is *more specific* than another ([§15.12.2.5](15.12.2.5.md)), where the second method is generic, it is necessary to test whether some instantiation of the second method's type parameters can be inferred to make the first method more specific than the second.

Let `m_1` be the first method and `m_2` be the second method. Where `m_2` has type parameters P_1, ..., P_p, let α_1, ..., α_p be inference variables, and let θ be the substitution `[`P_1:=α_1, ..., P_p:=α_p`]`.

Let `e_1`, ..., `e_k` be the argument expressions of the corresponding invocation. Then:

- If `m_1` and `m_2` are applicable by strict or loose invocation ([§15.12.2.2](15.12.2.2.md), [§15.12.2.3](15.12.2.3.md)), then let S_1, ..., S_k be the formal parameter types of `m_1`, and let T_1, ..., T_k be the result of θ applied to the formal parameter types of `m_2`.
- If `m_1` and `m_2` are applicable by variable arity invocation ([§15.12.2.4](15.12.2.4.md)), then let S_1, ..., S_k be the first *k* variable arity parameter types of `m_1`, and let T_1, ..., T_k be the result of θ applied to the first *k* variable arity parameter types of `m_2`.

Note that no substitution is applied to S_1, ..., S_k; even if `m_1` is generic, the type parameters of `m_1` are treated as type variables, not inference variables.

The process to determine if `m_1` is more specific than `m_2` is as follows:

- First, an initial bound set, B, is constructed from the declared bounds of P_1, ..., P_p, as specified in [§18.1.3](18.1.3.md).

- Second, for all *i* (1 ≤ *i* ≤ *k*), a set of constraint formulas or bounds is generated.

  If T_i is a proper type, the result is *true* if S_i is more specific than T_i for `e_i` ([§15.12.2.5](15.12.2.5.md)), and *false* otherwise. (Note that S_i is always a proper type.)

  Otherwise, if T_i is not a functional interface type, the constraint formula ‹S_i `<:` T_i› is generated.

  Otherwise, T_i is a parameterization of a functional interface, I. It must be determined whether S_i satisfies the following five conditions:

  - S_i is a functional interface type.
  - S_i is not a superinterface of I, nor a parameterization of a superinterface of I.
  - S_i is not a subinterface of I, nor a parameterization of a subinterface of I.
  - If S_i is an intersection type, at least one element of the intersection is not a superinterface of I, nor a parameterization of a superinterface of I.
  - If S_i is an intersection type, no element of the intersection is a subinterface of I, nor a parameterization of a subinterface of I.

  If all five conditions are true, then the following constraint formulas or bounds are generated (where U_1 ... U_k and R_1 are the parameter types and return type of the function type of the capture of S_i, and V_1 ... V_k and R_2 are the parameter types and return type of the function type of T_i):

  - If `e_i` is an explicitly typed lambda expression:

    - For all *j* (1 ≤ *j* ≤ *k*), ‹U_j = V_j›.
    - If R_2 is `void`, *true*.
    - Otherwise, if R_1 and R_2 are functional interface types, and neither interface is a subinterface of the other, and `e_i` has at least one result expression, then these rules are applied recursively to R_1 and R_2, for each result expression in `e_i`.
    - Otherwise, if R_1 is a primitive type and R_2 is not, and `e_i` has at least one result expression, and each result expression of `e_i` is a standalone expression ([§15.2](15.2.md)) of a primitive type, *true*.
    - Otherwise, if R_2 is a primitive type and R_1 is not, and `e_i` has at least one result expression, and each result expression of `e_i` is either a standalone expression of a reference type or a poly expression, *true*.
    - Otherwise, ‹R_1 `<:` R_2›.

  - If `e_i` is an exact method reference:

    - For all *j* (1 ≤ *j* ≤ *k*), ‹U_j = V_j›.
    - If R_2 is `void`, *true*.
    - Otherwise, if R_1 is a primitive type and R_2 is not, and the compile-time declaration for `e_i` has a primitive return type, *true*.
    - Otherwise if R_2 is a primitive type and R_1 is not, and the compile-time declaration for `e_i` has a reference return type, *true*.
    - Otherwise, ‹R_1 `<:` R_2›.

  - If `e_i` is a parenthesized expression, these rules are applied recursively to the contained expression.

  - If `e_i` is a conditional expression, these rules are applied recursively to each of the second and third operands.

  - Otherwise, *false*.

  If the five constraints on S_i are not satisfied, the constraint formula ‹S_i `<:` T_i› is generated instead.

- Third, if `m_2` is applicable by variable arity invocation and has *k*+1 parameters, then where S_{k+1} is the *k*+1'th variable arity parameter type of `m_1` and T_{k+1} is the result of θ applied to the *k*+1'th variable arity parameter type of `m_2`, the constraint ‹S_{k+1} `<:` T_{k+1}› is generated.

- Fourth, the generated bounds and constraint formulas are reduced and incorporated with B to produce a bound set B'.

  If B' does not contain the bound *false*, and resolution of all the inference variables in B' succeeds, then `m_1` is more specific than `m_2`.

  Otherwise, `m_1` is not more specific than `m_2`.
