---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 19 Edition"
java_version: 19
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/se19/html/jls-18.html#jls-18.5.4
id: "18.5.4"
url: https://jvm.obfuscated.software/jls/se19/18.5.4.html
raw: https://jvm.obfuscated.software/jls/se19/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 generated 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 S_i and T_i are not both functional interface types, the constraint formula ‹S_i `<:` T_i› is generated.

  Otherwise, if the interface of S_i is a superinterface or a subinterface of the interface of T_i (or, where S_i or T_i is an intersection type, some interface of S_i is a superinterface or a subinterface of some interface of T_i), the constraint formula ‹S_i `<:` T_i› is generated.

  Otherwise, let MT_S be the function type of the capture of S_i, let MT_S' be the function type of S_i (without capture), and let MT_T be the function type of T_i. If MT_S and MT_T have a different number of formal parameters or type parameters, or if MT_S and MT_S' do not have the same type parameters ([§8.4.4](8.4.4.md)), the result is *false*. Otherwise, the following constraint formulas or bounds are generated from the type parameters, formal parameter types, and return types of MT_S and MT_T:

  - Let A_1, ..., A_n be the type parameters of MT_S, and let B_1, ..., B_n be the type parameters of MT_T.

    Let θ' be the substitution `[`B_1:=A_1, ..., B_n:=A_n`]`. Then, for all *j* (1 ≤ *j* ≤ *n*):

    - If the bound of A_j mentions one of A_1, ..., A_n, and the bound of B_j is a not proper type, *false*.
    - Otherwise, where X is the bound of A_j and Y is the bound of B_j, ‹X = Y θ'›.

    If the bound A_j mentions one of A_1, ..., A_n, and the bound of B_j is not a proper type, then producing an equality constraint would raise the possibility of an inference variable being bounded by an out-of-scope type variable. Since instantiating an inference variable with an out-of-scope type variable is nonsensical, we prefer to avoid the situation by giving up immediately whenever the possibility arises. This simplification is not completeness-preserving. (The same comment applies to the treatment of formal parameter types and return types below.)

  - Let U_1, ..., U_k be the formal parameter types of MT_S, and let V_1, ..., V_k be the formal parameter types of MT_T. Then, for all *j* (1 ≤ *j* ≤ *k*):

    - If U_j mentions one of A_1, ..., A_n, and V_j is not a proper type, *false*.
    - Otherwise, ‹V_j θ' `<:` U_j›, and, where U_1', ..., U_k' are the formal parameter types of MT_S', and A_1', ..., A_n' are the type parameters of MT_S', ‹V_j`[`B_1:=A_1', ..., B_n:=A_n'`]` = U_j'›

  - Let R_S be the return type of MT_S, and let R_T be the return type of MT_T. Then:

    - If R_S mentions one of A_1, ..., A_n, and R_T is not a proper type, *false*.

    - Otherwise, if `e_i` is an explicitly typed lambda expression:

      - If R_T is `void`, *true*.
      - Otherwise, if R_S and R_T are functional interface types, and `e_i` has at least one result expression, then for each result expression in `e_i`, this entire second step is repeated to infer constraints under which R_S is more specific than R_T θ' for the given result expression.
      - Otherwise, if R_S is a primitive type and R_T 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_T is a primitive type and R_S 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_S `<:` R_T θ'›.

    - Otherwise, if `e_i` is an exact method reference:

      - If R_T is `void`, *true*.
      - Otherwise, if R_S is a primitive type and R_T is not, and the compile-time declaration for `e_i` has a primitive return type, *true*.
      - Otherwise if R_T is a primitive type and R_S is not, and the compile-time declaration for `e_i` has a reference return type, *true*.
      - Otherwise, ‹R_S `<:` R_T θ'›.

    - Otherwise, if `e_i` is a parenthesized expression, these rules for constraints derived from R_S and R_T are applied recursively for the contained expression.

    - Otherwise, if `e_i` is a conditional expression, these rules for constraints derived from R_S and R_T are applied recursively for each of the second and third operands.

    - Otherwise, if `e_i` is a `switch` expression, these rules for constraints derived from R_S and R_T are applied recursively for each of its result expressions.

    - Otherwise, *false*.

- 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`.
