1 Java Language Changes

This section summarizes the updated language features in Java SE 9 and subsequent releases.

Java Language Changes for Java SE 22

Feature Description JEP
Statements Before super(...) Introduced as a preview feature for this release.

In constructors in the Java programming language, you may add statements that don't reference the instance being created before an explicit constructor invocation.

JEP 447: Statements before super(...) (Preview)
Unnamed Variables and Patterns First previewed in Java SE 21 as Unnamed Patterns and Variables, this feature is permanent in this release. This means that it can be used in any program compiled for Java SE 22 without enabling preview features.

This feature remains unchanged since Java SE 21.

JEP 456: Unnamed Variables & Patterns
String Templates Preview feature from Java SE 21 re-previewed for this release.

Except for a technical change in the types of template expressions, which is described in String Templates (Second Preview) in The Java Language Specification: Java SE 22 Edition, this feature remains unchanged since Java SE 21.

JEP 459: String Templates (Second Preview)
Implicitly Declared Classes and Instance Main Methods First previewed in Java SE 21 as JEP 445: Unnamed Classes and Instance Main Methods (Preview), this feature is re-previewed for this release as JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview).
In this release:
  • A source file without an enclosing class declaration is said to implicitly declare a class with a name chosen by the host system.
  • The procedure for selecting a main method is simplified. If there is a candidate main method with a String[] parameter then we invoke that method; otherwise we invoke a candidate main method with no parameters.
JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview)

Java Language Changes for Java SE 21

Feature Description JEP
Record Patterns First previewed in Java SE 19, this feature is permanent in this release. This means that it can be used in any program compiled for Java SE 21 without enabling preview features.

In this release, support for record patterns appearing in the header of an enhanced for statement has been removed.

JEP 440: Record Patterns
Pattern Matching for switch Expressions and Statements First previewed in Java SE 17, this feature is permanent in this release. This means that it can be used in any program compiled for Java SE 21 without enabling preview features.

In this release:

  • Parenthesized patterns have been removed.
  • Qualified enum constants as case constants in switch expressions and statements are allowed.
JEP 441: Pattern Matching for switch
String Templates Introduced as a preview feature for this release.

String templates complement Java's existing string literals and text blocks by coupling literal text with embedded expressions and template processors to produce specialized results.

JEP 430: String Templates (Preview)
Unnamed Patterns and Variables Introduced as a preview feature for this release.

Unnamed patterns match a record component without stating the component's name or type. Unnamed variables are variables that can be initialized but not used. You denote both with the underscore character (_).

JEP 443: Unnamed Patterns and Variables (Preview)
Unnamed Classes and Instance Main Methods Introduced as a preview feature for this release.

Unnamed classes and instance main methods enable students to write streamlined declarations for single-class programs and then seamlessly expand their programs later to use more advanced features as their skills grow.

JEP 445: Unnamed Classes and Instance Main Methods (Preview)

Java Language Changes for Java SE 20

Feature Description JEP
Pattern Matching for switch Expressions and Statements

Preview feature from Java SE 17 re-previewed for this release.

In this release:

  • An exhaustive switch (that is, a switch expression or a pattern switch statement) over an enum class throws a MatchException instead of an IncompatibleClassChangeError if no switch label applies at run time.
  • The grammar for switch labels is simpler.
  • The compiler can infer the type of the type arguments for generic record patterns in all constructs that accept patterns: switch statements and expressions, instanceof expressions, and enhanced for statements.
JEP 433: Pattern Matching for switch (Fourth Preview)
Record Patterns

Preview feature from Java SE 19 re-previewed for this release.

In this release:

  • The compiler can infer the type of the type arguments for generic record patterns.
  • Record patterns can appear in an enhanced for statement.
  • Named record patterns are no longer supported.
JEP 432: Record Patterns (Second Preview)

Java Language Changes for Java SE 19

Feature Description JEP
Pattern Matching for switch Expressions and Statements

Preview feature from Java SE 17 re-previewed for this release.

In this release:

  • The syntax of a guarded pattern label consists of a pattern and a when clause.
  • If a selector expression evaluates to null and the switch block does not have null case label, then a NullPointerException is thrown, even if a pattern label can match the type of the null value.
  • If a switch expression or statement is exhaustive at compile time but not at run time, then a MatchException is thrown.
JEP 427: Pattern Matching for switch (Third Preview)
Record Patterns

Introduced as a preview feature for this release.

A record pattern consists of a type, a record component pattern list used to match against the corresponding record components, and an optional identifier. You can nest record patterns and type patterns to enable a powerful, declarative, and composable form of data navigation and processing.

JEP 405: Record Patterns (Preview)

Java Language Changes for Java SE 18

Feature Description JEP
Pattern Matching for switch Expressions and Statements

Preview feature from Java SE 17 re-previewed for this release.

In this release:

JEP 420: Pattern Matching for switch (Second Preview)

Java Language Changes for Java SE 17

Feature Description JEP
Sealed Classes

First previewed in Java SE 15, this feature is permanent in this release. This means that it can be used in any program compiled for Java SE 17 without enabling preview features.

A sealed class or interface restricts which classes or interfaces can extend or implement it.

JEP 409: Sealed Classes
Pattern Matching for switch Expressions and Statements

Introduced as a preview feature for this release.

Pattern matching for switch expressions and statements allows an expression to be tested against a number of patterns, each with a specific action, so that complex data-oriented queries can be expressed concisely and safely.

JEP 406: Pattern Matching for switch (Preview)

Java Language Changes for Java SE 16

Feature Description JEP
Sealed Classes

Preview feature from Java SE 15 re-previewed for this release. It has been enhanced with several refinements, including more strict checking of narrowing reference conversions with respect to sealed type hierarchies.

A sealed class or interface restricts which classes or interfaces can extend or implement it.
JEP 397: Sealed Classes (Second Preview)
Record Classes

First previewed in Java SE 14, this feature is permanent in this release. This means that it can be used in any program compiled for Java SE 16 without enabling preview features.

In this release, inner classes may declare members that are either explicitly or implicitly static. This includes record class members, which are implicitly static.

A record is a class that acts as transparent carrier for immutable data.

JEP 395: Records
Pattern Matching for instanceof

First previewed in Java SE 14, this feature is permanent in this release. This means that it can be used in any program compiled for Java SE 16 without enabling preview features.

In this release, pattern variables are no longer implicitly final, and it's a compile-time error if a pattern instanceof expression compares an expression of type S with a pattern of type T, where S is a subtype of T.

Pattern matching allows common logic in a program, namely the conditional extraction of components from objects, to be expressed more concisely and safely.

JEP 394: Pattern Matching for instanceof

Java Language Changes for Java SE 15

Feature Description JEP
Sealed Classes

Introduced as a preview feature for this release.

A sealed class or interface restricts which classes or interfaces can extend or implement it.

JEP 360: Sealed Classes (Preview)
Record Classes

Preview feature from Java SE 14 re-previewed for this release. It has been enhanced with support for local records.

A record is a class that acts as transparent carrier for immutable data.

JEP 384: Records (Second Preview)
Pattern Matching for instanceof

Preview feature from Java SE 14 re-previewed for this release. It is unchanged between Java SE 14 and this release.

Pattern matching allows common logic in a program, namely the conditional extraction of components from objects, to be expressed more concisely and safely.

JEP 375: Pattern Matching for instanceof (Second Preview)

Text Blocks

See also Programmer's Guide to Text Blocks

First previewed in Java SE 13, this feature is permanent in this release. This means that it can be used in any program compiled for Java SE 15 without enabling preview features.

A text block is a multiline string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over the format when desired.

JEP 378: Text Blocks

Java Language Changes for Java SE 14

Feature Description JEP
Pattern Matching for the instanceof Operator

Introduced as a preview feature for this release.

Pattern matching allows common logic in a program, namely the conditional extraction of components from objects, to be expressed more concisely and safely.

JEP 305: Pattern Matching for instanceof (Preview)JEP 305: Pattern Matching for instanceof (Preview)
Records

Introduced as a preview feature for this release.

Records provide a compact syntax for declaring classes which are transparent holders for shallowly immutable data.

JEP 359: Records (Preview)

Text Blocks

See also Programmer's Guide to Text Blocks

Preview feature from Java SE 13 re-previewed for this release. It has been enhanced with support for more escape sequences.

A text block is a multiline string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over the format when desired.

JEP 375: Pattern Matching for instanceof (Second Preview)
Switch Expressions

First previewed in Java SE 12, this feature is permanent in this release. This means that it can be used in any program compiled for Java SE 14 without needing to enable preview features.

This feature extends switch so it can be used as either a statement or an expression, and so that both forms can use either traditional case ... : labels (with fall through) or new case ... -> labels (with no fall through), with a further new statement for yielding a value from a switch expression.

JEP 361: Switch Expressions (Standard)

Java Language Changes for Java SE 13

Feature Description JEP
Text Blocks, see Programmer's Guide to Text Blocks

Introduced as a preview feature for this release.

A text block is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over format when desired.

JEP 355: Text Blocks (Preview)
Switch Expressions

Preview feature from Java SE 12 re-previewed for this release. It has been enhanced with one change: To specify the value of a switch expression, use the new yield statement instead of the break statement.

This feature extends switch so it can be used as either a statement or an expression, and so that both forms can use either traditional case ... : labels (with fall through) or new case ... -> labels (with no fall through), with a further new statement for yielding a value from a switch expression. .

JEP 354: Switch Expressions (Second Preview)

Java Language Changes for Java SE 12

Feature Description JEP
Switch Expressions

Introduced as a preview feature for this release.

This feature extends the switch statement so that it can be used as either a statement or an expression, and that both forms can use either a "traditional" or "simplified" scoping and control flow behavior.

JEP 325: Switch Expressions (Preview)

Java Language Changes for Java SE 11

Feature Description JEP

Local Variable Type Inference

See also Local Variable Type Inference: Style Guidelines

Introduced in Java SE 10. In this release, it has been enhanced with support for allowing var to be used when declaring the formal parameters of implicitly typed lambda expressions.

Local-Variable Type Inference extends type inference to declarations of local variables with initializers.

Java Language Changes for Java SE 10

Feature Description JEP

Local Variable Type Inference

See also Local Variable Type Inference: Style Guidelines

Introduced in this release.

Local-Variable Type Inference extends type inference to declarations of local variables with initializers.

JEP 286: Local-Variable Type Inference

Java Language Changes for Java SE 9

Feature Description JEP

Java Platform module system, see Project Jigsaw on OpenJDK.

Introduced in this release.

The Java Platform module system introduces a new kind of Java programing component, the module, which is a named, self-describing collection of code and data. Its code is organized as a set of packages containing types, that is, Java classes and interfaces; its data includes resources and other kinds of static information. Modules can either export or encapsulate packages, and they express dependencies on other modules explicitly.

Java Platform Module System (JSR 376)

Small language enhancements (Project Coin):

Introduced in Java SE 7 as Project Coin. It has been enhanced with a few amendments.

JEP 213: Milling Project Coin

JSR 334: Small Enhancements to the Java Programming Language

More Concise try-with-resources Statements

If you already have a resource as a final or effectively final variable, you can use that variable in a try-with-resources statement without declaring a new variable. An "effectively final" variable is one whose value is never changed after it is initialized.

For example, you declared these two resources:

        // A final resource
        final Resource resource1 = new Resource("resource1");
        // An effectively final resource
        Resource resource2 = new Resource("resource2");

In Java SE 7 or 8, you would declare new variables, like this:

        try (Resource r1 = resource1;
             Resource r2 = resource2) {
            ...
        }

In Java SE 9, you don’t need to declare r1 and r2:

// New and improved try-with-resources statement in Java SE 9
        try (resource1;
             resource2) {
            ...
        }

There is a more complete description of the try-with-resources statement in The Java Tutorials (Java SE 8 and earlier).

@SafeVarargs Annotation Allowed on Private Instance Methods

The @SafeVarargs annotation is allowed on private instance methods. It can be applied only to methods that cannot be overridden. These include static methods, final instance methods, and, new in Java SE 9, private instance methods.

Diamond Syntax and Anonymous Inner Classes

You can use diamond syntax in conjunction with anonymous inner classes. Types that can be written in a Java program, such as int or String, are called denotable types. The compiler-internal types that cannot be written in a Java program are called non-denotable types.

Non-denotable types can occur as the result of the inference used by the diamond operator. Because the inferred type using diamond with an anonymous class constructor could be outside of the set of types supported by the signature attribute in class files, using the diamond with anonymous classes was not allowed in Java SE 7.

Underscore Character Not Legal Name

If you use the underscore character ("_") as an identifier, your source code can no longer be compiled.

Support for Private Interface Methods

Private interface methods are supported. This support allows nonabstract methods of an interface to share code between them.