---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 17 Edition"
java_version: 17
chapter: "Chapter 18. Type Inference"
section: "18"
heading: "Chapter 18. Type Inference"
source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-18.html
id: "18"
url: https://jvm.obfuscated.software/jls/se17/18.html
raw: https://jvm.obfuscated.software/jls/se17/18.md
---

# Chapter 18. Type Inference

A variety of compile-time analyses require reasoning about types that are not yet known. Principal among these are generic method applicability testing ([§18.5.1](18.5.1.md)) and generic method invocation type inference ([§18.5.2](18.5.2.md)). In general, we refer to the process of reasoning about unknown types as *type inference*.

At a high level, type inference can be decomposed into three processes:

- *Reduction* takes a compatibility assertion about an expression or type, called a *constraint formula*, and reduces it to a set of *bounds* on *inference variables*. Often, a constraint formula reduces to *other* constraint formulas, which must be recursively reduced. A procedure is followed to identify these additional constraint formulas and, ultimately, to express via a bound set the conditions under which the choices for inferred types would render each constraint formula true.
- *Incorporation* maintains a set of inference variable bounds, ensuring that these are consistent as new bounds are added. Because the bounds on one variable can sometimes impact the possible choices for another variable, this process propagates bounds between such interdependent variables.
- *Resolution* examines the bounds on an inference variable and determines an *instantiation* that is compatible with those bounds. It also decides the order in which interdependent inference variables are to be resolved.

These processes interact closely: reduction can trigger incorporation; incorporation may lead to further reduction; and resolution may cause further incorporation.

- [§18.1](18.1.md) more precisely defines the concepts used as intermediate results and the notation used to express them.
- [§18.2](18.2.md) describes reduction in detail.
- [§18.3](18.3.md) describes incorporation in detail.
- [§18.4](18.4.md) describes resolution in detail.
- [§18.5](18.5.md) defines how these inference tools are used to solve certain compile-time analysis problems.

In comparison to the Java SE 7 Edition of *The Java® Language Specification*, important changes to inference include:

- Adding support for lambda expressions and method references as method invocation arguments.
- Generalizing to define inference in terms of poly expressions, which may not have well-defined types until *after* inference is complete. This has the notable effect of improving inference for nested generic method and diamond constructor invocations.
- Describing how inference is used to handle wildcard-parameterized functional interface target types and most specific method analysis.
- Clarifying the distinction between invocation applicability testing (which involves only the invocation arguments) and invocation type inference (which incorporates a target type).
- Delaying resolution of all inference variables, even those with lower bounds, until invocation type inference, in order to get better results.
- Improving inference behavior for interdependent (or self-dependent) variables.
- Eliminating bugs and potential sources of confusion. This revision more carefully and precisely handles the distinction between specific conversion contexts and subtyping, and describes reduction by paralleling the corresponding non-inference relations. Where there are intentional departures from the non-inference relations, these are explicitly identified as such.
- Laying a foundation for future evolution: enhancements to or new applications of inference will be easier to integrate into the specification.

**Sections:**

- [18.1. Concepts and Notation](18.1.md)
- [18.2. Reduction](18.2.md)
- [18.3. Incorporation](18.3.md)
- [18.4. Resolution](18.4.md)
- [18.5. Uses of Inference](18.5.md)
