java-se-language-updates
java-se-language-updates
Release 17
F40865-05
January 2025
Java Platform, Standard Edition Java Language Updates, Release 17
F40865-05
This software and related documentation are provided under a license agreement containing restrictions on use and
disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or
allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit,
perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation
of this software, unless required by law for interoperability, is prohibited.
The information contained herein is subject to change without notice and is not warranted to be error-free. If you find
any errors, please report them to us in writing.
If this is software, software documentation, data (as defined in the Federal Acquisition Regulation), or related
documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, then
the following notice is applicable:
U.S. GOVERNMENT END USERS: Oracle programs (including any operating system, integrated software, any
programs embedded, installed, or activated on delivered hardware, and modifications of such programs) and Oracle
computer documentation or other Oracle data delivered to or accessed by U.S. Government end users are "commercial
computer software," "commercial computer software documentation," or "limited rights data" pursuant to the applicable
Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use, reproduction,
duplication, release, display, disclosure, modification, preparation of derivative works, and/or adaptation of i) Oracle
programs (including any operating system, integrated software, any programs embedded, installed, or activated on
delivered hardware, and modifications of such programs), ii) Oracle computer documentation and/or iii) other Oracle
data, is subject to the rights and limitations specified in the license contained in the applicable contract. The terms
governing the U.S. Government's use of Oracle cloud services are defined by the applicable contract for such services.
No other rights are granted to the U.S. Government.
This software or hardware is developed for general use in a variety of information management applications. It is not
developed or intended for use in any inherently dangerous applications, including applications that may create a risk of
personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all
appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its
affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications.
Oracle®, Java, MySQL, and NetSuite are registered trademarks of Oracle and/or its affiliates. Other names may be
trademarks of their respective owners.
Intel and Intel Inside are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used
under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Epyc, and the AMD logo
are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open
Group.
This software or hardware and documentation may provide access to or information about content, products, and
services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all
warranties of any kind with respect to third-party content, products, and services unless otherwise set forth in an
applicable agreement between you and Oracle. Oracle Corporation and its affiliates will not be responsible for any loss,
costs, or damages incurred due to your access to or use of third-party content, products, or services, except as set forth
in an applicable agreement between you and Oracle.
Contents
Preface
Audience v
Documentation Accessibility v
Diversity and Inclusion v
Related Documents v
Conventions v
3 Preview Features
4 Sealed Classes
iii
5 Pattern Matching
Pattern Matching for instanceof 5-4
Pattern Matching for switch Expressions and Statements 5-6
6 Record Classes
7 Switch Expressions
8 Text Blocks
iv
Preface
This guide describes the updated language features in Java SE 9 and subsequent releases.
Audience
This document is for Java developers.
Documentation Accessibility
For information about Oracle's commitment to accessibility, visit the Oracle Accessibility
Program website at http://www.oracle.com/pls/topic/lookup?ctx=acc&id=docacc.
Related Documents
See JDK 17 Documentation.
Conventions
The following text conventions are used in this document:
Convention Meaning
boldface Boldface type indicates graphical user interface elements associated with an
action, or terms defined in text or the glossary.
italic Italic type indicates book titles, emphasis, or placeholder variables for which
you supply particular values.
v
Preface
Convention Meaning
monospace Monospace type indicates commands within a paragraph, URLs, code in
examples, text that appears on the screen, or text that you enter.
vi
1
Java Language Changes Summary
The following tables summarize new Java language features since Java SE 9.
1-1
Chapter 1
• : Permanent feature
Feature 17 16 15 14 13 12 11 10 9
Sealed Classes
JEP JEP JEP
409 397 360
Record Classes
JEP JEP JEP
395 384 359
Pattern Matching for instanceof
JEP JEP JEP
394 375 305
Text Blocks
JEP JEP JEP
378 368 355
Switch Expressions
JEP JEP JEP
361 354 325
Local-Variable Syntax for Lambda Parameters: see Local Variable
Type Inference JEP
323
1-2
Chapter 1
Feature 17 16 15 14 13 12 11 10 9
Local Variable Type Inference
JEP
286
Java Platform Module System: see Project Jigsaw on OpenJDK
JSR
376
Milling Project Coin: see Java Language Changes for Java SE 9
JEP
213
Small Enhancements to the Java Programming Language: see
Java Language Changes for Java SE 9 JSR
334
1-3
2
Java Language Changes
This section summarizes the updated language features in Java SE 9 and subsequent
releases.
2-1
Chapter 2
Java Language Changes for Java SE 15
2-2
Chapter 2
Java Language Changes for Java SE 14
2-3
Chapter 2
Java Language Changes for Java SE 12
2-4
Chapter 2
Java Language Changes for Java SE 9
// A final resource
final Resource resource1 = new Resource("resource1");
// An effectively final resource
Resource resource2 = new Resource("resource2");
2-5
Chapter 2
Java Language Changes for Java SE 9
There is a more complete description of the try-with-resources statement in The Java Tutorials
(Java SE 8 and earlier).
2-6
3
Preview Features
A preview feature is a new feature whose design, specification, and implementation are
complete, but which is not permanent, which means that the feature may exist in a different
form or not at all in future JDK releases.
Introducing a feature as a preview feature in a mainline JDK release enables the largest
developer audience possible to try the feature out in the real world and provide feedback. In
addition, tool vendors are encouraged to build support for the feature before Java developers
use it in production. Developer feedback helps determine whether the feature has any design
mistakes, which includes hard technical errors (such as a flaw in the type system), soft
usability problems (such as a surprising interaction with an older feature), or poor architectural
choices (such as one that forecloses on directions for future features). Through this feedback,
the feature's strengths and weaknesses are evaluated to determine if the feature has a long-
term role in the Java SE Platform, and if so, whether it needs refinement. Consequently, the
feature may be granted final and permanent status (with or without refinements), or undergo a
further preview period (with or without refinements), or else be removed.
Every preview feature is described by a JDK Enhancement Proposal (JEP) that defines its
scope and sketches its design. For example, JEP 325 describes the JDK 12 preview feature
for switch expressions. For background information about the role and lifecycle of preview
features, see JEP 12.
For example, suppose you have an application named MyApp.java that uses the JDK 12
preview language feature switch expressions. Compile this with JDK 12 as follows:
Note:
When you compile an application that uses preview features, you'll receive a warning
message similar to the following:
Remember that preview features are subject to change and are intended to provoke
feedback.
3-1
Chapter 3
To run an application that uses preview features from JDK release n, use java from JDK
release n with the --enable-preview option. To continue the previous example, to run MyApp,
run java from JDK 12 as follows:
Note:
Code that uses preview features from an older release of the Java SE Platform will
not necessarily compile or run on a newer release.
The tools jshell and javadoc also support the --enable-preview command-line option.
Sending Feedback
You can provide feedback on preview features, or anything else about the Java SE Platform,
as follows:
• If you find any bugs, then submit them at Java Bug Database.
• If you want to provide substantive feedback on the usability of a preview feature, then post
it on the OpenJDK mailing list where the feature is being discussed. To find the mailing list
of a particular feature, see the feature's JEP page and look for the label Discussion. For
example, on the page JEP 325: Switch Expressions (Preview), you'll find "Discussion
amber dash dev at openjdk dot java dot net" near the top of the page.
• If you are working on an open source project, then see Quality Outreach on the OpenJDK
Wiki.
3-2
4
Sealed Classes
Sealed classes and interfaces restrict which other classes or interfaces may extend or
implement them.
For background information about sealed classes and interfaces, see JEP 409.
One of the primary purposes of inheritance is code reuse: When you want to create a new
class and there is already a class that includes some of the code that you want, you can derive
your new class from the existing class. In doing this, you can reuse the fields and methods of
the existing class without having to write (and debug) them yourself.
However, what if you want to model the various possibilities that exist in a domain by defining
its entities and determining how these entities should relate to each other? For example, you're
working on a graphics library. You want to determine how your library should handle common
geometric primitives like circles and squares. You've created a Shape class that these
geometric primitives can extend. However, you're not interested in allowing any arbitrary class
to extend Shape; you don't want clients of your library declaring any further primitives. By
sealing a class, you can specify which classes are permitted to extend it and prevent any other
arbitrary class from doing so.
Define the following three permitted subclasses, Circle, Square, and Rectangle, in the same
module or in the same package as the sealed class:
4-1
Chapter 4
Alternatively, you can define permitted subclasses in the same file as the sealed class. If you
do so, then you can omit the permits clause:
package com.example.geometry;
4-2
Chapter 4
package com.example.graphics;
package com.example.expressions;
4-3
Chapter 4
package com.example.records.expressions;
4-4
Chapter 4
The cast expression Polygon p = (Polygon) r is allowed because it's possible that the
Rectangle value r could be of type Polygon; Rectangle is a subtype of Polygon. However,
consider this example:
Even though the class Triangle and the interface Polygon are unrelated, the cast expression
Polygon p = (Polygon) t is also allowed because at run time these types could be related. A
developer could declare the following class:
4-5
Chapter 4
However, there are cases where the compiler can deduce that there are no values (other than
the null reference) shared between two types; these types are considered disjoint. For
example:
Because the class UtahTeapot is final, it's impossible for a class to be a descendant of both
Polygon and UtahTeapot. Therefore, Polygon and UtahTeapot are disjoint, and the cast
statement Polygon p = (Polygon) u isn't allowed.
The compiler has been enhanced to navigate any sealed hierarchy to check if your cast
statements are allowed. For example:
The first cast statement UtahTeapot u = (UtahTeapot) s isn't allowed; a Shape can only be a
Polygon because Shape is sealed. However, as Polygon is non-sealed, it can be extended.
However, no potential subtype of Polygon can extend UtahTeapot as UtahTeapot is final.
Therefore, it's impossible for a Shape to be a UtahTeapot.
In contrast, the second cast statement Ring r = (Ring) s is allowed; it's possible for a Shape
to be a Ring because Ring is not a final class.
4-6
5
Pattern Matching
Pattern matching involves testing whether an object has a particular structure, then extracting
data from that object if there's a match. You can already do this with Java; however, pattern
matching introduces new language enhancements that enable you to conditionally extract data
from objects with code that's more concise and robust.
interface Shape { }
record Rectangle(double length, double width) implements Shape { }
record Circle(double radius) implements Shape { }
...
public static double getPerimeter(Shape shape) throws
IllegalArgumentException {
if (shape instanceof Rectangle r) {
return 2 * r.length() + 2 * r.width();
} else if (shape instanceof Circle c) {
return 2 * c.radius() * Math.PI;
} else {
throw new IllegalArgumentException("Unrecognized shape");
}
}
A pattern is a combination of a test, which is called a predicate; a target; and a set of local
variables, which are called pattern variables:
• The predicate is a Boolean-valued function with one argument; in this case, it’s the
instanceof operator testing whether the Shape argument is a Rectangle or a Circle.
• The target is the argument of the predicate, which is the Shape value.
• The pattern variables are those that store data from the target only if the predicate returns
true, which are the variables r and s.
See Pattern Matching for instanceof for more information.
5-1
Chapter 5
Note:
This is a preview feature, which is a feature whose design, specification, and
implementation are complete, but is not permanent, which means that the feature
may exist in a different form or not at all in future Java SE releases. To compile and
run code that contains preview features, you must specify additional command-line
options. See Preview Language and VM Features.
For background information about pattern matching for switch expressions and
statements, see JEP 406.
The following example also calculates the perimeter only for instances of Rectangle or Circle.
However, it uses a switch expression instead of an if-then-else statement:
Note:
This feature is part of JEP 406, which is a preview feature.
A guarded pattern enables a pattern to be refined with a boolean expression. A value matches
a guarded pattern if it matches the pattern and the boolean expression evaluates to true.
Consider the following example:
5-2
Chapter 5
}
}
You can move the boolean expression s.length == 1 into the case label with a guarded
pattern:
The first pattern matches if obj is both a String and of length 1. The second patten matches if
obj is a String of a different length.
A guarded patten has the form p && e where p is a pattern and e is a boolean expression. The
scope of a pattern variable that appears in p includes e. This lets you specify patterns such as
String s && (s.length() > 1), which matches a value that can be cast to a String whose
length is greater than one.
Parenthesized Patterns
Note:
This feature is part of JEP 406, which is a preview feature.
This example compiles. However, if you want to make it clear that the first arrow token (->) is
part of the case label and not part of a lambda expression, you can surround the guarded
pattern with parentheses:
5-3
Chapter 5
Pattern Matching for instanceof
Consider the following code that calculates the perimeter of certain shapes:
5-4
Chapter 5
Pattern Matching for instanceof
3. A destructuring, extracting either the length and width or the radius from the Shape object
Pattern matching enables you to remove the conversion step by changing the second operand
of the instanceof operator with a type pattern, making your code shorter and easier to read:
Note:
Removing this conversion step also makes your code safer. Testing an object's type
with the instanceof, then assigning that object to a new variable with a cast can
introduce coding errors in your application. You might change the type of one of the
objects (either the tested object or the new variable) and accidentally forget to
change the type of the other object.
A pattern is a combination of a predicate, or test, that can be applied to a target and a set of
local variables, called pattern variables, that are assigned values extracted from the target only
if the test is successful. The predicate is a Boolean-valued function of one argument; in this
case, it’s the instanceof operator testing whether the Shape argument is a Rectangle or a
Circle. The target is the argument of the predicate, which is the Shape value. The pattern
variables are those that store data from the target only if the predicate returns true, which are
the variables r and s.
A type pattern consists of a predicate that specifies a type, along with a single pattern variable.
In this example, the type patterns are Rectangle r and Circle c.
5-5
Chapter 5
Pattern Matching for switch Expressions and Statements
The scope of a pattern variable can extend beyond the statement that introduced it:
Because the conditional-AND operator (&&) is short-circuiting, the program can reach the
r.length() > 5 expression only if the instanceof operator is true.
Conversely, you can't pattern match with the instanceof operator in this situation:
The program can reach the r.length() || 5 if the instanceof is false; thus, you cannot use
the pattern variable r here.
Note:
This is a preview feature, which is a feature whose design, specification, and
implementation are complete, but is not permanent, which means that the feature
may exist in a different form or not at all in future Java SE releases. To compile and
run code that contains preview features, you must specify additional command-line
options. See Preview Language and VM Features.
For background information about pattern matching for switch expressions and
statements, see JEP 406.
5-6
Chapter 5
Pattern Matching for switch Expressions and Statements
Consider the following code that calculates the perimeter of certain shapes from the section
Pattern Matching for instanceof:
interface Shape { }
record Rectangle(double length, double width) implements Shape { }
record Circle(double radius) implements Shape { }
...
public static double getPerimeter(Shape shape) throws
IllegalArgumentException {
if (shape instanceof Rectangle r) {
return 2 * r.length() + 2 * r.width();
} else if (shape instanceof Circle c) {
return 2 * c.radius() * Math.PI;
} else {
throw new IllegalArgumentException("Unrecognized shape");
}
}
You can rewrite this code to use a pattern switch expression as follows:
5-7
Chapter 5
Pattern Matching for switch Expressions and Statements
The first pattern label case CharSequence cs dominates the second pattern label case String
s because every value that matches the pattern String s also matches the pattern
CharSequence cs but not the other way around. It's because String is a subtype of
CharSequence.
You'll get a compile-time error if any pattern dominates a subsequent pattern in a switch block.
There are two labels that match all values: the default label and a total type pattern (see Null-
Matching case Labels). You can't have more than one of these two labels in a switch block.
5-8
Chapter 5
Pattern Matching for switch Expressions and Statements
However, the type coverage of the case label default is all types, so the following example
compiles:
The compiler takes into account whether the type of a selector expression is a sealed class.
The following switch expression compiles. It doesn't need a default case label because its
type coverage is the classes A, B, and C, which are the only permitted subclasses of S, the type
of the selector expression:
In a switch expression, you can use a pattern variable inside the expression, block, or throw
statement that appears right of the arrow. For example:
5-9
Chapter 5
Pattern Matching for switch Expressions and Statements
}
System.out.println("Character, value " + c.charValue());
}
case Integer i ->
System.out.println("Integer: " + i);
default ->
throw new IllegalStateException("Invalid argument");
}
}
The scope of pattern variable c is the block to the right of case Character c ->. The scope of
pattern variable i is the println statement to the right of case Integer I ->.
In a switch statement, you can use a case label's pattern variable in its switch-labeled
statement group. However, you can't use it in any other switch-labeled statement group, even
if the program flow can fall through a default statement group. For example:
The scope of pattern variable c consists of the case Character c statement group: the two if
statements and the println statement that follows them. The scope doesn't include the
default statement group even though the switch statement can execute the case Character
c statement group, fall through the default case label, and then execute the default
statement group.
You will get a compile-time error if it's possible to fall through a case label that declares a
pattern variable. The following example doesn't compile:
5-10
Chapter 5
Pattern Matching for switch Expressions and Statements
If this code were allowed, and the value of the selector expression, obj, was a Character, then
the switch statement can execute the case Character c statement group, then fall through
the case Integer i case label, where the pattern variable i would have not been initialized.
Similarly, you can't declare multiple pattern variables in a case label. The following aren't
permitted; either c or i would have been initialized (depending on the value of obj).
This example prints null! when obj is null instead of throwing a NullPointerException.
Second, a pattern label whose pattern is a total type pattern matches null if the value of the
selector expression is null. A type pattern, T t, is total for a type S if the type erasure of S is a
subtype of the type erasure of T. For example, the type pattern Object obj is total for the type
String. Consider the following switch statement:
String s = ...
switch (s) {
case Object obj -> ... // total type pattern, so it matches null!
}
If a selector expression evaluates to null and the switch block does not have a pattern label
that is null-matching, then a NullPointerException is thrown as normal.
5-11
6
Record Classes
Record classes, which are a special kind of class, help to model plain data aggregates with
less ceremony than normal classes.
For background information about record classes, see JEP 395.
A record declaration specifies in a header a description of its contents; the appropriate
accessors, constructor, equals, hashCode, and toString methods are created automatically. A
record's fields are final because the class is intended to serve as a simple "data carrier".
For example, here is a record class with two fields:
A record class declaration consists of a name; optional type parameters (generic record
declarations are supported); a header, which lists the "components" of the record; and a body.
A record class declares the following members automatically:
• For each component in the header, the following two members:
– A private final field with the same name and declared type as the record
component. This field is sometimes referred to as a component field.
6-1
Chapter 6
– A public accessor method with the same name and type of the component; in the
Rectangle record class example, these methods are Rectangle::length() and
Rectangle::width().
• A canonical constructor whose signature is the same as the header. This constructor
assigns each argument from the new expression that instantiates the record class to the
corresponding component field.
• Implementations of the equals and hashCode methods, which specify that two record
classes are equal if they are of the same type and contain equal component values.
• An implementation of the toString method that includes the string representation of all the
record class's components, with their names.
As record classes are just special kinds of classes, you create a record object (an instance of a
record class) with the new keyword, for example:
Repeating the record class's components in the signature of the canonical constructor can be
tiresome and error-prone. To avoid this, you can declare a compact constructor whose
signature is implicit (derived from the components automatically).
For example, the following compact constructor declaration validates length and width in the
same way as in the previous example:
This succinct form of constructor declaration is only available in a record class. Note that the
statements this.length = length; and this.width = width; which appear in the canonical
constructor do not appear in the compact constructor. At the end of a compact constructor, its
6-2
Chapter 6
implicit formal parameters are assigned to the record class's private fields corresponding to its
components.
If you implement your own accessor methods, then ensure that they have the same
characteristics as implicitly derived accessors (for example, they're declared public and have
the same return type as the corresponding record class component). Similarly, if you
implement your own versions of the equals, hashCode, and toString methods, then ensure
that they have the same characteristics and behavior as those in the java.lang.Record class,
which is the common superclass of all record classes.
You can declare static fields, static initializers, and static methods in a record class, and they
behave as they would in a normal class, for example:
// Static field
static double goldenRatio;
// Static initializer
static {
goldenRatio = (1 + Math.sqrt(5)) / 2;
}
// Static method
public static Rectangle createGoldenRectangle(double width) {
return new Rectangle(width, width * goldenRatio);
}
}
You cannot declare instance variables (non-static fields) or instance initializers in a record
class.
For example, the following record class declaration doesn't compile:
6-3
Chapter 6
You can declare instance methods in a record class, independent of whether you implement
your own accessor methods. You can also declare nested classes and interfaces in a record
class, including nested record classes (which are implicitly static). For example:
• You can declare a record class that implements one or more interfaces, for example:
• You can annotate a record class and its individual components, for example:
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface GreaterThanZero { }
record Rectangle(
@GreaterThanZero double length,
@GreaterThanZero double width) { }
6-4
Chapter 6
If you annotate a record component, then the annotation may be propagated to members
and constructors of the record class. This propagation is determined by the contexts in
which the annotation interface is applicable. In the previous example, the
@Target(ElementType.FIELD) meta-annotation means that the @GreaterThanZero
annotation is propagated to the field corresponding to the record component.
Consequently, this record class declaration would be equivalent to the following normal
class declaration:
import java.time.*;
import java.util.*;
import java.util.stream.*;
List<Merchant> findTopMerchants(
List<Sale> sales, List<Merchant> merchants, int year, Month month) {
return merchants.stream()
.map(merchant -> new MerchantSales(
merchant, this.computeSales(sales, merchant, year, month)))
6-5
Chapter 6
List<Merchant> topMerchants =
app.findTopMerchants(salesList, merchantList, 2020,
Month.NOVEMBER);
System.out.println("Top merchants: ");
topMerchants.stream().forEach(m -> System.out.println(m.name()));
}
}
Like nested record classes, local record classes are implicitly static, which means that their
own methods can't access any variables of the enclosing method, unlike local classes, which
are never static.
6-6
Chapter 6
In Java SE 16 and later, an inner class may declare members that are either explicitly or
implicitly static, which includes record class members. The following example demonstrates
this:
Record Serialization
You can serialize and deserialize instances of record classes, but you can't customize the
process by providing writeObject, readObject, readObjectNoData, writeExternal,
or readExternal methods. The components of a record class govern serialization, while the
canonical constructor of a record class governs deserialization. See Serializable Records for
more information and an extended example. See also the section Serialization of Records in
the Java Object Serialization Specification.
You might get a compiler error if your source file imports a class named Record from a
package other than java.lang. A Java source file automatically imports all the types in the
java.lang package though an implicit import java.lang.*; statement. This includes the
java.lang.Record class, regardless of whether preview features are enabled or disabled.
package com.myapp;
6-7
Chapter 6
package org.example;
import com.myapp.*;
Both Record in the com.myapp package and Record in the java.lang package are imported
with a wildcard. Consequently, neither class takes precedence, and the compiler generates an
error when it encounters the use of the simple name Record.
To enable this example to compile, change the import statement so that it imports the fully
qualified name of Record:
import com.myapp.Record;
Note:
The introduction of classes in the java.lang package is rare but necessary from
time to time, such as Enum in Java SE 5, Module in Java SE 9, and Record in Java
SE 14.
6-8
Chapter 6
6-9
7
Switch Expressions
Like all expressions, switch expressions evaluate to a single value and can be used in
statements. They may contain "case L ->" labels that eliminate the need for break statements
to prevent fall through. You can use a yield statement to specify the value of a switch
expression.
For background information about the design of switch expressions, see JEP 361.
// ...
int numLetters = 0;
Day day = Day.WEDNESDAY;
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
numLetters = 6;
break;
case TUESDAY:
numLetters = 7;
break;
case THURSDAY:
case SATURDAY:
numLetters = 8;
break;
case WEDNESDAY:
numLetters = 9;
break;
default:
throw new IllegalStateException("Invalid day: " + day);
}
System.out.println(numLetters);
It would be better if you could "return" the length of the day's name instead of storing it in the
variable numLetters; you can do this with a switch expression. Furthermore, it would be better
if you didn't need break statements to prevent fall through; they are laborious to write and easy
to forget. You can do this with a new kind of case label. The following is a switch expression
that uses the new kind of case label to print the number of letters of a day of the week:
7-1
Chapter 7
When the Java runtime matches any of the labels to the left of the arrow, it runs the code to the
right of the arrow and does not fall through; it does not run any other code in the switch
expression (or statement). If the code to the right of the arrow is an expression, then the value
of that expression is the value of the switch expression.
You can use the new kind of case label in switch statements. The following is like the first
example, except it uses "case L ->" labels instead of "case L:" labels:
int numLetters = 0;
Day day = Day.WEDNESDAY;
switch (day) {
case MONDAY, FRIDAY, SUNDAY -> numLetters = 6;
case TUESDAY -> numLetters = 7;
case THURSDAY, SATURDAY -> numLetters = 8;
case WEDNESDAY -> numLetters = 9;
default -> throw new IllegalStateException("Invalid day: " + day);
};
System.out.println(numLetters);
A "case L ->" label along with its code to its right is called a switch labeled rule.
7-2
Chapter 7
yield 9;
default:
throw new IllegalStateException("Invalid day: " + day);
};
System.out.println(numLetters);
The previous example uses yield statements. They take one argument, which is the value that
the case label produces in a switch expression.
The yield statement makes it easier for you to differentiate between switch statements and
switch expressions. A switch statement, but not a switch expression, can be the target of a
break statement. Conversely, a switch expression, but not a switch statement, can be the
target of a yield statement.
Note:
It's recommended that you use "case L ->" labels. It's easy to forget to insert break
or yield statements when using "case L:" labels; if you do, you might introduce
unintentional fall through in your code.
For "case L ->" labels, to specify multiple statements or code that are not
expressions or throw statements, enclose them in a block. Specify the value that the
case label produces with the yield statement:
Exhaustiveness
Unlike switch statements, the cases of switch expressions must be exhaustive, which means
that for all possible values, there must be a matching switch label. Thus, switch expressions
normally require a default clause. However, for enum switch expressions that cover all known
constants, the compiler inserts an implicit default clause.
7-3
Chapter 7
In addition, a switch expression must either complete normally with a value or complete
abruptly by throwing an exception. For example, the following code doesn't compile because
the switch labeled rule doesn't contain a yield statement:
The following example doesn't compile because the switch labeled statement group doesn't
contain a yield statement:
i = switch (day) {
case MONDAY, TUESDAY, WEDNESDAY:
yield 0;
default:
System.out.println("Second half of the week");
// ERROR! Group doesn't contain a yield statement
};
Because a switch expression must evaluate to a single value (or throw an exception), you
can't jump through a switch expression with a break, yield, return, or continue statement,
like in the following example:
z:
for (int i = 0; i < MAX_VALUE; ++i) {
int k = switch (e) {
case 0:
yield 1;
case 1:
yield 2;
default:
continue z;
// ERROR! Illegal jump through a switch expression
};
// ...
}
7-4
8
Text Blocks
See Programmer's Guide to Text Blocks for more information about this language feature. For
background information about text blocks, see JEP 378.
8-1
9
Local Variable Type Inference
In JDK 10 and later, you can declare local variables with non-null initializers with the var
identifier, which can help you write code that’s easier to read.
Consider the following example, which seems redundant and is hard to read:
You can rewrite this example by declaring the local variables with the var identifier. The type of
the variables are inferred from the context:
var is a reserved type name, not a keyword, which means that existing code that uses var as a
variable, method, or package name is not affected. However, code that uses var as a class or
interface name is affected and the class or interface needs to be renamed.
var can be used for the following types of variables:
for (var counter = 0; counter < 10; counter++) {...} // infers int
• try-with-resources variable:
9-1
Chapter 9
In JDK 11 and later, you can declare each formal parameter of an implicitly typed lambda
expression with the var identifier:
9-2