1 Java Language Changes

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

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.