Java Se Language Updates
Java Se Language Updates
Release 23
F95819-03
January 2025
Java Platform, Standard Edition Java Language Updates, Release 23
F95819-03
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 vi
Documentation Accessibility vi
Diversity and Inclusion vi
Related Documents vi
Conventions vi
iii
3 Preview Features
7 Sealed Classes
8 Pattern Matching
Pattern Matching with instanceof 8-2
Scope of Pattern Variables and instanceof 8-4
Pattern Matching with switch 8-5
When Clauses 8-6
Pattern Label Dominance 8-7
Scope of Pattern Variables and switch 8-9
Null case Labels 8-11
Type Patterns 8-12
Type Patterns with Reference Types 8-12
Type Patterns with Primitive Types 8-13
Type Patterns with Parameterized Types 8-14
Record Patterns 8-16
Generic Record Patterns 8-17
Using var in Record Patterns 8-17
Primitive Types in Record Patterns 8-18
Nested Record Patterns 8-19
9 Record Classes
The Canonical Constructor of a Record Class 9-2
Alternative Record Constructors 9-3
iv
Explicit Declaration of Record Class Members 9-3
Features of Record Classes 9-5
Record Classes and Sealed Classes and Interfaces 9-6
Local Record Classes 9-6
Static Members of Inner Classes 9-7
Differences Between the Serialization of Records and Ordinary Objects 9-8
Record Serialization Principles 9-8
How Record Serialization Works 9-9
APIs Related to Record Classes 9-12
12 Text Blocks
v
Preface
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 23 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.
vi
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.
vii
1
Java Language Changes Summary
The following tables summarize new Java language features since Java SE 9.
1-1
Chapter 1
1-2
Chapter 1
specifies whether the feature was made permanent, withdrawn for the specified release, or
available as a preview feature:
• : Permanent feature
Feature 23 22 21 20 19 18 17
Module Import Declarations
JEP
476
Primitive Types in Patterns, instanceof, and switch
JEP
455
Flexible Constructor Bodies
JEP JEP
482 447
Simple Source Files and Instance Main Methods
JEP JEP JEP
477 463 445
Unnamed Variables and Patterns
JEP JEP
456 443
String Templates
JEP JEP
459 430
Record Patterns
JEP JEP JEP
440 432 405
Pattern Matching with switch
JEP JEP JEP JEP JEP
441 433 427 420 406
Sealed Classes
JEP
409
Feature 16 15 14 13 12 11 10 9
Sealed Classes
JEP JEP
397 360
Record Classes
JEP JEP JEP
395 384 359
1-3
Chapter 1
Feature 16 15 14 13 12 11 10 9
Pattern Matching with instanceof
JEP JEP JEP
394 375 305
Text Blocks
JEP JEP JEP
378 368 355
Switch Expressions and Statements
JEP JEP JEP
361 354 325
Local-Variable Syntax for Lambda Parameters: see Local Variable Type
Inference JEP
323
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-4
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 22
Note:
String Templates were first previewed in JDK 21 (JEP 430) and re-previewed in JDK
22 (JEP 459). String Templates were intended to re-preview again in JDK 23 (JEP
465). However, after feedback and extensive discussion, we concluded that the
feature is unsuitable in its current form. There is no consensus on what a better
design will be; therefore, we have withdrawn the feature for now, and JDK 23 will not
include it.
See March 2024 Archives by thread and Update on String Templates (JEP 459) from
the Project Amber amber-spec-experts mailing list for further discussion.
2-2
Chapter 2
Java Language Changes for Java SE 21
2-3
Chapter 2
Java Language Changes for Java SE 20
2-4
Chapter 2
Java Language Changes for Java SE 19
2-5
Chapter 2
Java Language Changes for Java SE 18
2-6
Chapter 2
Java Language Changes for Java SE 17
2-7
Chapter 2
Java Language Changes for Java SE 16
2-8
Chapter 2
Java Language Changes for Java SE 15
2-9
Chapter 2
Java Language Changes for Java SE 13
2-10
Chapter 2
Java Language Changes for Java SE 10
// A final resource
final Resource resource1 = new Resource("resource1");
// An effectively final resource
Resource resource2 = new Resource("resource2");
2-11
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-12
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
Module Import Declarations
You can import all packages exported by a module with one statement.
Note:
This is a preview feature. A preview feature is a feature whose design, specification,
and implementation are complete, but is not permanent. A preview 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 more information about module import
declarations, see JEP 476
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
You can replace the four single-type-import declarations in this example with type-import-on-
demand declarations. However, you still need three of them:
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
4-1
Chapter 4
import module M;
It imports, on demand, all of the public top-level class and interfaces in the following:
• The packages exported by the module M to the current module.
• The packages exported by the modules that are read by the current module due to reading
the module M. This enables a program to use the API of a module, which might refer to
classes and interfaces from other modules, without having to import all those other
modules.
For example, the module import declaration import module java.sql has the same effect
as import java.sql.* plus on-demand imports for the indirect exports of the java.sql
module, which include the packages java.logging and java.xml.
Ambiguous Imports
It's possible to import classes with the same simple name from different packages with module
import declarations. However, this can lead to compile-time errors. The following example uses
both java.awt.List and java.util.List:
import java.awt.Frame;
import java.awt.Label;
import java.awt.List;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
AWTExample(java.util.List<String> fruits) {
Frame f = new Frame();
Label l = new Label("Fruits");
List lst = new List();
fruits.forEach(i -> lst.add(i));
l.setBounds(20, 40, 80, 30);
lst.setBounds(20, 70, 80, 80);
f.add(l);
f.add(lst);
f.setSize(200,200);
f.setTitle("Fruit");
f.setLayout(null);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
4-2
Chapter 4
}
}
Suppose you replace the single-type-import declarations with these module import
declarations:
void main() {
String[] fruits = new String[] { "apple", "berry", "citrus" };
Map<String, String> m = Stream
.of(fruits)
.collect(Collectors.toMap(
s -> s.toUpperCase().substring(0,1),
Function.identity()));
m.forEach((k, v) ->
4-3
Chapter 4
4-4
5
Flexible Constructor Bodies
In constructors, you may add statements that don't reference the instance being created before
an explicit constructor invocation.
Note:
This is a preview feature. A preview feature is a feature whose design, specification,
and implementation are complete, but is not permanent. A preview 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 flexible constructor bodies, see JEP 482.
You can use this feature to prepare arguments for a superclass constructor by performing
nontrivial computations or to validate arguments you want to pass to a superclass constructor.
The following example validates whether the argument value is positive before passing it to
the superclass constructor:
The prologue of the constructor's body consists of the statements that appear before the
super(...) invocation. The epilogue of the constructor's body consists of the statements that
follow the super(...) invocation.
In the previous example, the early construction context of PositiveBigInteger consists of the
argument Long.toString(value) and the if-statement that checks whether value is positive.
Code in an early construction context may not access the instance under construction. This
means you can’t have the following in an early construction context:
5-1
Chapter 5
Early Construction Context
• Any unqualified this expression: Note that you don't need to use the this keyword to
access the instance under construction. For example:
class A {
int i;
A() {
// Error: Cannot reference this before supertype constructor has
been
// called
this.i++;
• Any field access, method invocation, or method reference qualified by super: Again,
note that you don't need to use the super keyword to access the superclass of the instance
under construction:
class D {
int j;
}
class E extends D {
E() {
// Error: cannot reference super before supertype constructor has
been
called
super.j++;
5-2
Chapter 5
Early Construction Context
Consider the following example, which consists of two classes: Super and Sub, which extends
Super and overrides Super::overridenMethod:
class Super {
Super() { overriddenMethod(); }
void overriddenMethod() { System.out.println("hello"); }
}
final int x;
Sub(int x) {
// The Super constructor is implicitly invoked,
// which calls overriddenMethod(), before initializing
// the field x in Sub.
this.x = x;
}
@Override
void overriddenMethod() { System.out.println(x); }
0
42
When the example invokes the constructor for Sub, it implicitly invokes the constructor for
Super before assigning a value to the field x in Sub. As a result, when the example invokes
Sub:overriddenMethod in the constructor for Sub, it prints the uninitialized value of x, which is
0.
You can initialize the field x in Sub and then invoke super() afterward:
final int x;
BetterSub(int x) {
// Initialize the int x field in BetterSub before
// invoking the Super constructor with super().
5-3
Chapter 5
Early Construction Context
this.x = x;
super();
}
@Override
void overriddenMethod() { System.out.println(x); }
42
42
Nested Classes
A nested class is a member of its enclosing class, which means you can't access a nested
class from its enclosing class's early construction context. For example:
class B {
class C { }
B() {
// Error: cannot reference this before supertype constructor has been
// called
new C();
super();
}
}
However, a nested class's enclosing class is not one of its members, which means you can
access its enclosing class from its early construction context. In the following example, both
accessing F's member variable f and method hello() in the early construction context of its
nested class G is permitted:
class F {
int f;
void hello() {
System.out.println("Hello!");
}
class G {
G() {
F.this.f++;
hello();
super();
}
5-4
Chapter 5
Early Construction Context
}
}
Records
Record constructors may not invoke super(...). However, noncanonical constructors must
involve a canonical constructor by invoking this(...). Statements may appear before
this(...).
Remember that a canonical constructor is a constructor whose signature is the same as the
record's component list. It initializes all the component fields of the record class. Alternative or
noncanonical record constructors have argument lists that don't match the record's type
parameters.
In the following example, the record RectanglePair contains a noncanonical constructor,
RectanglePair(Pair<Float> corner). Because it's a noncanonical constructor, it must invoke
a canonical constructor with this(...). It contains several statements before this(...) that
retrieve both values from the Pair<Float> parameter and validate that these values aren't
negative:
5-5
6
Implicitly Declared Classes and Instance Main
Methods
The features Instance Main Methods and Implicitly Declared Classes enable students to write
their first programs without needing to understand the full set of language features designed for
large programs.
Note:
This is a preview feature. A preview feature is a feature whose design, specification,
and implementation are complete, but is not permanent. A preview 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 instance main methods and implicitly declared
classes, see JEP 477.
The Java programming language excels in developing large, complex applications developed
and maintained over many years by large teams. It has rich features for data hiding, reuse,
access control, namespace management, and modularity which allow components to be
cleanly composed while being developed and maintained independently. The composition of
large components is called programming-in-the-large.
However, the Java programming language is also intended to be a first language and offers
many constructs that are useful for programming-in-the-small (everything that is internal to a
component). When programmers first start out, they do not write large programs in a team —
they write small programs by themselves. At this stage, there is no need for the programming-
in-the-large concepts of classes, packages, and modules.
When teaching programming, instructors start with the basic programming-in-the-small
concepts of variables, control flow, and subroutines. There is no need for the programming-in-
the-large concepts of classes, packages, and modules. Students who are learning to program
have no need for encapsulation and namespaces which are useful later to separately evolve
components written by different people.
Instance main methods and implicitly declared classes enhance the Java programming
language's support for programming in the small as follows:
• Enhance the protocol by which Java programs are launched by allowing instance main
methods that are not static, need not be public, and need not have a String[]
parameter. See Flexible Launch Protocol.
• Allow a compilation unit (a source file) to implicitly declare a class. See 7.3. Compilation
Units in the Java Language Specification and Implicitly Declared Classes.
• Automatically import methods for simple textual I/O with the console and public top-level
classes and interfaces of the packages exported by the java.base module. See
Automatic Import of the Static Methods of java.io.IO and the Module java.base.
6-1
Chapter 6
Flexible Launch Protocol
Consider the classic HelloWorld program that is often used as the first program for Java
students:
Topics
• Flexible Launch Protocol
• Implicitly Declared Classes
• Automatic Import of the Static Methods of java.io.IO and the Module java.base
• Growing a Program
6-2
Chapter 6
Implicitly Declared Classes
The actions of choosing the class containing the main method, assembling its dependencies in
the form of a module path or a class path (or both), loading the class, initializing it, and
invoking the main method with its arguments constitute the launch protocol. With JEP 463, the
launch protocol has been enhanced to offer more flexibility in the declaration of a program's
entry point and, in particular, to allow instance main methods, as follows:
• Allows the main method of a launched class to have public, protected, or default (such
as package) access.
• If a launched class contains a main method with a String[] parameter, then it chooses
that method.
• If a launched class contains a main method without parameters, then it chooses that
method.
• In either case, if the chosen main method is static then it invokes it.
• Otherwise, the chosen main method is an instance main method and the launched class
must have a zero-parameter, non-private constructor (public, protected, or package
access). It invokes that constructor and then invokes the main method of the resulting
object. If there is no such constructor, then it reports an error and terminates.
• If there is no suitable main method then it reports an error and terminates.
By using instance main methods, we can simplify the HelloWorld program presented in
Implicitly Declared Classes and Instance Main Methods to:
class HelloWorld {
void main() {
System.out.println("Hello, World!");
}
}
6-3
Chapter 6
Implicitly Declared Classes
Even though every method resides in a class, we can stop requiring explicit class declarations
for code that doesn't need it — just as we don't require explicit package or module declarations
for code that don't need them.
Beginning with JEP 463, when the Java compiler encounters a source file containing a method
not enclosed in a class declaration, it considers that method, any similar methods, and any
unenclosed fields and classes in the file to form the body of an implicitly declared top-level
class.
An implicitly declared class (also known as an implicit class) is always a member of the
unnamed package. It is also final and doesn't implement any interface or extend any class
other than Object. An implicit class can't be referenced by name, so there can be no method
references to its static methods. However, the this keyword can still be used, as well as
method references to instance methods.
The code of an implicit class can't refer to the implicit class by name, so instances of an implicit
class can't be constructed directly. Such a class is useful only as a standalone program or as
an entry point to a program. Therefore, an implicit class must have a main method that can be
launched as described in Flexible Launch Protocol. This requirement is enforced by the Java
compiler.
An implicit class resides in the unnamed package, and the unnamed package resides in the
unnamed module. While there can only be one unnamed package (barring multiple class
loaders) and only one unnamed module, there can be multiple implicit classes in the unnamed
module. Every implicit class contains a main method and represents a program. Consequently,
multiple implicit classes in an unnamed package represent multiple programs.
An implicit class is similar to an explicitly declared class. Its members can have the same
modifiers (such as private and static) and the modifiers have the same defaults (such as
package access and instance membership). One key difference is that while an implicit class
has a default zero-parameter constructor, it can have no other constructor.
With these changes, we can now write the HelloWorld program as:
void main() {
System.out.println("Hello, World!");
}
Because top-level members are interpreted as members of the implicit class, we can also write
the program as:
void main() {
System.out.println(greeting());
}
void main() {
System.out.println(greeting);
}
6-4
Chapter 6
Automatic Import of the Static Methods of java.io.IO and the Module java.base
You can launch a source file named HelloWorld.java that contains an implicit class with the
java command-line tool as follows:
$ java HelloWorld.java
The Java compiler compiles that file to the launchable class file HelloWorld.class. In this
case, the compiler chooses HelloWorld for the class name as an implementation detail.
However, that name still can't be used directly in Java source code.
At this time, the javadoc tool can't generate API documentation for an implicit class because
implicit classes don't define an API that is accessible from other classes. However, fields and
methods of an implicit class can generate API documentation.
void main() {
println(greeting);
}
In addition, every implicitly declared class imports, on demand, all public top-level classes and
interfaces in all packages exported by the java.base module. It is as if the module import
declaration import module java.base appears at the beginning of every implicitly declared
class. See Module Import Declarations for more information.
The following example is an implicitly declared class that requires no import declarations for
Map, Stream, Collectors, or Function as they are contained in packages exported by the
java.base module.
void main() {
String[] fruits = new String[] { "apple", "berry", "citrus" };
Map<String, String> m = Stream
.of(fruits)
.collect(Collectors.toMap(
s -> s.toUpperCase().substring(0,1),
6-5
Chapter 6
Growing a Program
Function.identity()));
m.forEach((k, v) -> println(k + " " + v));
}
Growing a Program
By omitting the concepts and constructs it doesn't need, a HelloWorld program written as an
implicit class is more focused on what the program actually does. Even so, all members
continue to be interpreted just as they are in an ordinary class.
Concepts and constructs can easily be added to an implicit class as needed by the program.
To evolve an implicit class into an ordinary class, all we need to do is wrap its declaration,
excluding import statements, inside an explicit class declaration.
6-6
7
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:
7-1
Chapter 7
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;
7-2
Chapter 7
package com.example.graphics;
package com.example.expressions;
7-3
Chapter 7
package com.example.records.expressions;
7-4
Chapter 7
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:
7-5
Chapter 7
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.
7-6
8
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.
A pattern describes a test that can be performed on a value. Patterns appear as operands of
statements and expressions, which provide the values to be tested. For example, consider this
expression:
s instanceof Rectangle r
The pattern Rectangle r is an operand of the instanceof expression. It's testing if the
argument s has the type given in the pattern, which is Rectangle. Sometimes, the argument
that a pattern tests is called the target.
Note:
When the operand to the right of instanceof is a pattern, like the previous example,
then instanceof is the pattern match operator.
When the operand to the right of instanceof is a type, then instanceof is the type
comparison operator. The following example uses instanceof as the type
comparison operator:
s instanceof Rectangle
A pattern can declare zero or more pattern variables. For example, the pattern Rectangle r
declares only one, r.
The process of testing a value against a pattern is called pattern matching. If a value
successfully matches a pattern, then the pattern variables are initialized with data from the
target. In this example, if s is a Rectangle, then s is converted to a Rectangle and then
assigned to r.
Patterns can also appear in the case labels of a switch statement or expression. For example:
8-1
Chapter 8
Pattern Matching with instanceof
}
}
A type pattern consists of a type along with a single pattern variable. In this example,
Rectangle r is a type pattern.
A record pattern consists of a record type and a (possibly empty) record pattern list. For
example, consider this record declaration and expression:
The record pattern Point(double x, double y) tests whether the target obj is a
Point(double, double). If so, it extracts the x and y values from obj directly and assigns
them to the pattern variables a and b, respectively.
Topics
• Pattern Matching with instanceof
• Pattern Matching with switch
• Type Patterns
• Record Patterns
Consider the following code that calculates the perimeter of certain shapes:
8-2
Chapter 8
Pattern Matching with instanceof
Note:
Removing this conversion step also makes your code safer. Testing an object's type
with instanceof and 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. See Safe Casting with instanceof and switch for
more information.
8-3
Chapter 8
Pattern Matching with instanceof
The pattern Rectangle r is an operand of the instanceof expression. It's testing if its target s
has the type given in the pattern, which is Rectangle. Similarly, the expression s instanceof
Circle c tests if s has the type Circle.
If a value successfully matches a pattern, then the pattern variables are initialized with data
from the target. In this example, if the target s is a Rectangle, then s is converted to a
Rectangle and then assigned to r. Similarly, if s is a Circle, then it's converted to a Circle
and then assigned to c,
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:
8-4
Chapter 8
Pattern Matching with switch
The program can reach the r.length() || 5 if the instanceof is false; thus, you cannot use
the pattern variable r here.
See Scope of Pattern Variables and switch for more examples of where you can use a pattern
variable.
interface Shape { }
record Rectangle(double length, double width) implements Shape { }
record Circle(double radius) implements Shape { }
// ...
You can rewrite this code to use a pattern switch expression as follows:
8-5
Chapter 8
Pattern Matching with switch
case Rectangle r:
return 2 * r.length() + 2 * r.width();
case Circle c:
return 2 * c.radius() * Math.PI;
default:
throw new IllegalArgumentException("Unrecognized shape");
}
}
Topics
• When Clauses
• Pattern Label Dominance
• Scope of Pattern Variables and switch
• Null case Labels
When Clauses
You can add a Boolean expression right after a pattern label with a when clause. This is called
a guarded pattern label. The Boolean expression in the when clause is called a guard. A value
matches a guarded pattern label if it matches the pattern and, if so, the guard also evaluates to
true. Consider the following example:
You can move the Boolean expression s.length == 1 right after the case label with a when
clause:
The first pattern label (which is a guarded pattern label) matches if obj is both a String and of
length 1. The second patten label matches if obj is a String of a different length.
8-6
Chapter 8
Pattern Matching with switch
A guarded patten label has the form p when e where p is a pattern and e is a Boolean
expression. The scope of any pattern variable declared in p includes e.
The following example also demonstrates when clauses. It uses a primitive type, double, for its
switch expression's selector expression:
Note:
Guards containing primitive types is a preview feature. A preview feature is a feature
whose design, specification, and implementation are complete, but is not permanent.
A preview 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.
See JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview) for
additional information.
void bigNumbers(long v) {
switch (v) {
case long x when x < 1_000_000L ->
System.out.println("Less than a million");
case long x when x < 1_000_000_000L ->
System.out.println("Less than a billion");
case long x when x < 1_000_000_000_000L ->
System.out.println("Less than a trillion");
case long x when x < 1_000_000_000_000_000L ->
System.out.println("Less than a quadrillion");
default -> System.out.println("At least a quadrillion");
}
}
8-7
Chapter 8
Pattern Matching with switch
the compiler raises an error if a pattern label can never match because a preceding one will
always match first. The following example results in a compile-time error:
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.
A pattern label can dominate a constant label. These examples cause compile-time errors:
8-8
Chapter 8
Pattern Matching with switch
Note:
Guarded pattern labels don't dominate constant labels. For example:
Although the value 1 matches both the guarded pattern label case Integer i when
i > 0 and the constant label case 1, the guarded pattern label doesn't dominate the
constant label. Guarded patterns aren't checked for dominance because they're
generally undecidable. Consequently, you should order your case labels so that
constant labels appear first, followed by guarded pattern labels, and then followed by
nonguarded pattern labels:
8-9
Chapter 8
Pattern Matching with switch
}
}
The scope of pattern variable c includes the when clause of the case label that contains the
declaration of c.
• The expression, block, or throw statement that appears to the right of the arrow of the case
label:
The scope of pattern variable c includes the block to the right of case Character c ->.
The scope of pattern variable i includes the println statement to the right of case
Integer i ->.
• The switch-labeled statement group of a case label:
The scope of pattern variable c includes 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 label, and then execute the
default statement group.
8-10
Chapter 8
Pattern Matching with switch
Note:
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:
If this code were allowed, and the value of the selector expression, obj, were a Character,
then the switch statement can execute the case Character c statement group and then
fall through the case Integer i label, where the pattern variable i would have not been
initialized.
See Scope of Pattern Variables and instanceof for more examples of where you can use a
pattern variable.
This example prints null! when obj is null instead of throwing a NullPointerException.
You may not combine a null case label with anything but a default case label. The following
generates a compiler error:
8-11
Chapter 8
Type Patterns
If a selector expression evaluates to null and the switch block does not have null case label,
then a NullPointerException is thrown as normal. Consider the following switch statement:
String s = null;
switch (s) {
case Object obj -> System.out.println("This doesn't match null");
// No null label; NullPointerException is thrown
// if s is null
}
Although the pattern label case Object obj matches objects of type String, this example
throws a NullPointerException. The selector expression evaluates to null, and the switch
expression doesn't contain a null case label.
Type Patterns
A type pattern consists of a type along with a single pattern variable. It's used to test whether a
value is an instance of the type appearing in the pattern.
Topics
• Type Patterns with Reference Types
• Type Patterns with Primitive Types
• Type Patterns with Parameterized Types
8-12
Chapter 8
Type Patterns
Note:
Primitive types in type patterns is a preview feature. A preview feature is a feature
whose design, specification, and implementation are complete, but is not permanent.
A preview 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.
See JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview) for
additional information.
The following example uses type patterns to test whether a value is a float, double, or int:
float v = 1000.01f;
See the section Decimal ↔ Binary Conversion Issues in the Double JavaDoc API
documentation for an explanation why the converted double value is different than the original
float value in this example.
Tip:
Using pattern matching with primitives is particularly useful for ensuring type safety
when casting values. See Safe Casting with instanceof and switch for more
information.
8-13
Chapter 8
Type Patterns
The case labels in the following switch expression use type patterns that involve float:
Consider the following example where the instanceof operator tests whether myList, a List,
has the type List<String>:
8-14
Chapter 8
Type Patterns
The following expressions return true. The type parameters on both sides of the instanceof
pattern matching operator are the same:
• sb instanceof Box<String> s
• sb instanceof Shoebox<String> s
The expression sb instanceof Shoebox<?> s returns true. The wildcard type (?) matches all
types.
However, the compiler can't fully validate at runtime whether Shoebox<Object> can be cast to
Shoebox<String> even though String is a subtype of Object. This parameterized type
information won't exist at run time because of type erasure. Consequently, the compiler
generates an error for the following expression:
sb instanceof Shoebox<Object> s
8-15
Chapter 8
Record Patterns
Note:
The previous example uses instanceof as the pattern matching operator. If you use
instanceof as the type comparison operator, the same issues about parameterized
types and type erasure still apply. For example, the compiler generates an error for
this expression:
sb instanceof Shoebox<Object>
Record Patterns
You can use a record pattern to test whether a value is an instance of a record class type (see
Record Classes) and, if it is, to recursively perform pattern matching on its component values.
For background information about record patterns, see JEP 440.
The following example tests whether obj is an instance of the Point record with the record
pattern Point(double x, double y):
In addition, this example extracts the x and y values from obj directly, automatically calling the
Point record's accessor methods.
A record pattern consists of a type and a (possibly empty) record pattern list. In this example,
the type is Point and the pattern list is (double x, double y).
Note:
The null value does not match any record pattern.
The following example is the same as the previous one except it uses a type pattern instead of
a record pattern:
8-16
Chapter 8
Record Patterns
Topics
• Generic Record Patterns
• Using var in Record Patterns
• Primitive Types in Record Patterns
• Nested Record Patterns
record Box<T>(T t) { }
// ...
You can test whether a value is an instance of a parameterized record type provided that the
value could be cast to the record type in the pattern without requiring an unchecked
conversion. The following example doesn't compile:
In the following example, the compiler infers that the pattern variables x and y are of type
double:
8-17
Chapter 8
Record Patterns
The following example is equivalent to printBoxContents. The compiler infers its type
argument and pattern variable: Box(var s) is inferred as Box<String>(String s)
Note:
Support for pattern matching on primitive types of a record's components is a preview
feature. A preview feature is a feature whose design, specification, and
implementation are complete, but is not permanent. A preview 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.
For more information, see JEP 455: Primitive Types in Patterns, instanceof, and
switch (Preview).
In the following example, the record Cup contains one component of type double. The example
creates a Cup that contains a float and then tests whether the Cup contains a double, float,
or int value with the instanceof operator:
record Cup(double d) { }
//...
8-18
Chapter 8
Record Patterns
Similarly, as described in Type Patterns with Primitive Types, a value of a record's component
matches the corresponding type pattern if it's safe to cast the value to the type in the type
pattern. A cast is safe if there's no loss of information or exception thrown during the cast or
conversion. For example, casting a float to a double or float is safe; casting a float to an
int isn't. (See the section Decimal ↔ Binary Conversion Issues in the Double JavaDoc API
documentation for an explanation why the converted double value is different than the original
float value in this example.)
You can do the same for parameterized records. The compiler infers the types of the record
pattern's type arguments and pattern variables. In the following example, the compiler infers
Box(Box(var s)) as Box<Box<String>>(Box(String s)).
8-19
9
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.
9-1
Chapter 9
The Canonical Constructor of a Record Class
– 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:
Topics
• The Canonical Constructor of a Record Class
• Alternative Record Constructors
• Explicit Declaration of Record Class Members
• Features of Record Classes
• Record Classes and Sealed Classes and Interfaces
• Local Record Classes
• Static Members of Inner Classes
• Differences Between the Serialization of Records and Ordinary Objects
• APIs Related to Record Classes
9-2
Chapter 9
Alternative Record Constructors
this.length = length;
this.width = width;
}
}
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
implicit formal parameters are assigned to the record class's private fields corresponding to its
components.
9-3
Chapter 9
Explicit Declaration of Record Class Members
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:
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:
9-4
Chapter 9
Features of Record Classes
public RotationAngle {
angle = Math.toRadians(angle);
}
}
• 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) { }
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:
9-5
Chapter 9
Record Classes and Sealed Classes and Interfaces
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)))
.sorted((m1, m2) -> Double.compare(m2.sales(), m1.sales()))
.map(MerchantSales::merchant)
.collect(Collectors.toList());
}
9-6
Chapter 9
Static Members of Inner Classes
s.date().getYear() == yr &&
s.date().getMonth() == mo)
.mapToDouble(s -> s.value())
.sum();
}
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.
9-7
Chapter 9
Differences Between the Serialization of Records and Ordinary Objects
Topics
• Record Serialization Principles
• How Record Serialization Works
9-8
Chapter 9
Differences Between the Serialization of Records and Ordinary Objects
components only. As a result of this restriction, programmers more easily understand the
serialized form of a record: it consists of the state components of the record.
The second point relates to the mechanics of the deserialization process. Suppose
deserialization is reading the bytes of an object for a normal class (not a record class).
Deserialization would create a new object by invoking the no-args constructor of a superclass,
then use reflection to set the object’s fields to values deserialized from the stream. This is
insecure because the normal class has no opportunity to validate the values coming from the
stream. The result may be an “impossible” object that could never be created by an ordinary
Java program using constructors. With records, deserialization works differently.
Deserialization creates a new record object by invoking a record class’s canonical constructor,
passing values deserialized from the stream as arguments to the canonical constructor. This is
secure because it means the record class can validate the values before assigning them to
fields, just like when an ordinary Java program creates a record object via new. “Impossible”
objects are impossible. This is achievable because the record components, the canonical
constructor, and the serialized form are all known and consistent.
Note the verbose boilerplate code for the equals, hashCode, and toString methods.
9-9
Chapter 9
Differences Between the Serialization of Records and Ordinary Objects
The following example is the equivalent record counterpart of RangeClass. You make a record
class serializable in the same way as a normal class, by implementing Serializable.
Note that you don’t have to add any additional boilerplate to RangeRecord to make it
serializable. Specifically, you don’t need to add a serialVersionUID field because the
serialVersionUID of a record class is 0L unless explicitly declared, and the requirement for
matching the serialVersionUID value is waived for record classes.
Rarely, for migration compatibility between normal classes and record classes, a
serialVersionUID may be declared. See the section 5.6.2 Compatible Changes in Java
Object Serialization Specification for more information.
The following examples serialize a RangeClass object and RangeRecord object, both with the
same high-end and low-end values.
import java.io.*;
import java.io.*;
RangeClass[lo=100, hi=1]
RangeRecord[lo=100, hi=1]
Oops! Did you manage to spot the mistake? The low-end value is higher than the high-end
value. This shouldn’t be allowed. An integer range implementation should have this invariant:
the low end of the range can be no higher than the high end.
9-10
Chapter 9
Differences Between the Serialization of Records and Ordinary Objects
Note that RangeRecord uses the compact version of the canonical constructor declaration,
which enables you to omit the boilerplate assignments.
With the updated RangeClass and RangeRecord examples, the Deserialize example prints
output similar to the following:
RangeClass[lo=100, hi=1]
Exception in thread "main" java.io.InvalidObjectException: 100, 1
at java.base/
java.io.ObjectInputStream.readRecord(ObjectInputStream.java:2296)
at java.base/
java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2183)
at java.base/
java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1685)
at java.base/
java.io.ObjectInputStream.readObject(ObjectInputStream.java:499)
at java.base/
java.io.ObjectInputStream.readObject(ObjectInputStream.java:457)
at Deserialize.main(Deserialize.java:9)
Caused by: java.lang.IllegalArgumentException: 100, 1
at RangeRecord.<init>(RangeRecord.java:6)
at java.base/
java.io.ObjectInputStream.readRecord(ObjectInputStream.java:2294)
... 5 more
The example attempts to deserialize the stream objects in the serial.data file even though
RangeClass and RangeRecord have been created with a low-end value of 100 and a high-end
value of 1.
The example demonstrates that a normal class and a record class are deserialized differently:
• A RangeClass object was deserialized even though the newly created object violates the
constructor invariant. This may seem counterintuitive at first, but as described earlier,
9-11
Chapter 9
APIs Related to Record Classes
deserialization of an object whose class is a normal class (not a record class) creates the
object by invoking the no-args constructor of the (first non-serializable) superclass, which
in this case is java.lang.Object. Of course, it would not be possible for the example
Serialize to generate such a byte stream for a RangeClass object because the example
must use the two-arg constructor with its invariant checking. However, remember
deserialization operates on just a stream of bytes, and these bytes can, in some cases,
come from almost anywhere.
• However, the RangeRecord stream object failed to deserialize because its stream field
values for the low end and high end violate the invariant check in the constructor. This is
nice, and actually what we want: deserialization proceeds through the canonical
constructor.
The fact that a serializable class can have a new object created without one of its constructors
being invoked is often overlooked, even by experienced developers. An object created by
invoking a distant no-args constructor can lead to unexpected behavior at run time, since
invariant checks in the deserialized class’s constructor are not performed. However,
deserialization of a record object cannot be exploited to create an “impossible” object.
See the section 1.13 Serialization of Records in Java Object Serialization Specification for
more information.
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;
package org.example;
import com.myapp.*;
9-12
Chapter 9
APIs Related to Record Classes
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.
9-13
10
Unnamed Variables and Patterns
Unnamed variables are variables that can be initialized but not used. Unnamed patterns can
appear in a pattern list of a record pattern, and always match the corresponding record
component. You can use them instead of a type pattern. They remove the burden of having to
write a type and name of a pattern variable that's not needed in subsequent code. You denote
both with the underscore character (_).
For background information about unnamed variables and patterns, see JEP 456.
Unnamed Variables
You can use the underscore keyword (_) as the name of a local variable, exception, or lambda
parameter in a declaration when the value of the declaration isn't needed. This is called an
unnamed variable, which represents a variable that’s being declared but it has no usable
name.
Unnamed variables are useful when the side effect of a statement is more important than its
result.
Consider the following example that iterates through the elements of the array orderIDs with a
for loop. The side effect of this for loop is that it calculates the number of elements in
orderIDs without ever using the loop variable id:
You can use an unnamed variable to omit or elide the unused variable id:
The following table describes where you can declare an unnamed variable:
10-1
Chapter 10
for (int i = 0, _ =
sideEffect.apply("Starting for-loop");
i < 10; i++) {
System.out.println(i);
}
10-2
Chapter 10
Unnamed Patterns
Consider the following example that calculates the distance between two instances of
ColoredPoint:
10-3
Chapter 10
java.lang.Math.pow(p2.x - p1.x, 2) +
java.lang.Math.pow(p2.y - p1.y, 2));
} else {
return -1;
}
}
The example doesn't use the Color component of the ColoredPoint record. To simplify the
code and improve readability, you can elide the type patterns Color c1 and Color c2 with the
unnamed pattern (_):
Alternatively, you can keep the type pattern's type and elide just its name:
No value is bound to the unnamed pattern variable. Consequently, the highlighted statement in
the following example is invalid:
10-4
Chapter 10
void printSalary(Employee b) {
switch (b) {
case Salaried r -> System.out.println("Salary: " + r.salary());
case Freelancer _ -> System.out.println("Other");
case Intern _ -> System.out.println("Other");
}
}
You may use multiple patterns in a case label provided that they don't declare any pattern
variables. For example, you can rewrite the previous switch statement as follows:
switch (b) {
case Salaried r -> System.out.println("Salary: " +
r.salary());
case Freelancer _, Intern _ -> System.out.println("Other");
}
10-5
11
Switch Expressions and Statements
You can use the switch keyword as either a statement or an expression. Like all expressions,
switch expressions evaluate to a single value and can be used in statements. Switch
expressions 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.
Topics
• Arrow Cases
• Colon Cases and the the yield Statement
• Qualified enum Constants as case Constants
• Primitive Values in switch Expressions and Statements
• Exhaustiveness of switch
• Completion and switch Expressions
Arrow Cases
Consider the following switch statement that prints the number of letters of a day of the week:
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;
}
System.out.println(numLetters);
11-1
Chapter 11
Colon Cases and the the yield Statement
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 an arrow case. The following is a switch expression that uses
arrow cases to print the number of letters of a day of the week:
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 arrow cases in switch statements. The following is like the first example, except it
uses arrow cases instead of colon cases:
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;
};
System.out.println(numLetters);
An arrow case along with its code to its right is called a switch-labeled rule.
11-2
Chapter 11
Colon Cases and the the yield Statement
case TUESDAY:
System.out.println(7);
yield 7;
case THURSDAY:
case SATURDAY:
System.out.println(8);
yield 8;
case WEDNESDAY:
System.out.println(9);
yield 9;
};
System.out.println(numLetters);
The previous example uses yield statements. They take one argument, which is the value that
the colon case 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 arrow cases. It's easy to forget to insert break or
yield statements when using colon cases; if you do, you might introduce
unintentional fall through in your code.
For arrow cases, to specify multiple statements or code that are not expressions or
throw statements, enclose them in a block. Specify the value that the arrow case
produces with the yield statement:
11-3
Chapter 11
Qualified enum Constants as case Constants
Consider the following switch expression whose selector expression is an enum type:
In the following example, the type of the selector expression is an interface that's been
implemented by two enum types. Because the type of the selector expression isn't an enum
type, this switch expression uses guarded patterns instead:
11-4
Chapter 11
Primitive Values in switch Expressions and Statements
However, switch expressions and statements allow qualified enum constants, so you could
rewrite this example as follows:
Therefore, you can use an enum constant when the type of the selector expression is not an
enum type provided that the enum constant's name is qualified and its value is assignment-
compatible with the type of the selector expression.
Note:
This is a preview feature. A preview feature is a feature whose design, specification,
and implementation are complete, but is not permanent. A preview 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.
See JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview) for
additional information.
If a selector expression is of type long, float, double, and boolean, then its case labels must
have the same type as the selector expression or its corresponding boxed type. For example:
void whichFloat(float v) {
switch (v) {
case 0f ->
System.out.println("Zero");
case float x when x > 0f && x <= 10f ->
System.out.println(x + " is between 1 and 10");
case float x ->
System.out.println(x + " is larger than 10");
}
}
11-5
Chapter 11
Exhaustiveness of switch
If you change the case label 0f to 0, you would get the following compile-time error:
error: constant label of type int is not compatible with switch selector type
float
You can't use two floating-point literals as case labels that are representationally equivalent.
(See the JavaDoc API documentation for the Double class for more information about
representation equivalence.) The following example generates a compiler error. The value
0.999999999f is representationally equivalent to 1.0f:
void duplicateLabels(float v) {
switch (v) {
case 1.0f -> System.out.println("One");
// error: duplicate case label
case 0.999999999f -> System.out.println("Almost one");
default -> System.out.println("Another float value");
}
}
Switching on boolean values is a useful alternative to the ternary conditional operator (?:)
because a boolean switch expression or statement can contain statements as well as
expressions. For example, the following code uses a boolean switch expression to perform
some logging when false:
Exhaustiveness of switch
The cases of a switch expression or statement must be exhaustive, which means that for all
possible values, there must be a matching case label. Thus, a switch expression or statement
normally require a default clause. However, for an enum switch expression that covers all
known constants, the compiler inserts an implicit default clause, like the examples at the
beginning of this section that print the number of letters in name of a day of the week.
The cases of a switch statement must be exhaustive if it uses pattern or null labels. See
Pattern Matching with switch and Null case Labels for more information.
The following switch expression is not exhaustive and generates a compile-time error. The
type coverage of its labels consist of the subtypes of String and Integer. However, it doesn't
include the type of the selector expression, Object:
11-6
Chapter 11
Exhaustiveness of switch
However, the type coverage of the case label default is all types, so the following example
compiles:
Consequently, for a switch expression or statement to be exhaustive, the type coverage of its
labels must include the type of the selector expression.
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:
The compiler can also determine the type coverage of a switch expression or statement if the
type of its selector expression is a generic sealed class. The following example compiles. The
only permitted subclasses of interface I are classes A and B. However, because the selector
expression is of type I<Integer>, the switch block requires only class B in its type coverage to
be exhaustive:
11-7
Chapter 11
Exhaustiveness of switch
The type of a switch expression or statement's selector expression can also be a generic
record. As always, a switch expression or statement must be exhaustive. The following
example doesn't compile. No match for a Pair exists that contains two values, both of type A:
record Pair<T>(T x, T y) {}
class A {}
class B extends A {}
The following example compiles. Interface I is sealed. Types C and D cover all possible
instances:
record Pair<T>(T x, T y) {}
sealed interface I permits C, D {}
record C(String s) implements I {}
record D(String s) implements I {}
// ...
If a switch expression or statement is exhaustive at compile time but not at run time, then a
MatchException is thrown. This can happen when a class that contains an exhaustive switch
expression or statement has been compiled, but a sealed hierarchy that is used in the analysis
of the switch expression or statement has been subsequently changed and recompiled. Such
changes are migration incompatible and may lead to a MatchException being thrown when
running the switch statement or expression. Consequently, you need to recompile the class
containing the switch expression or statement.
class PrintA {
public static void main(String[] args) {
System.out.println(switch (OnlyAB.getAValue()) {
case A a -> 1;
case B b -> 2;
});
11-8
Chapter 11
Completion and switch Expressions
}
}
The switch expression in the class PrintA is exhaustive and this example compiles. When you
run PrintA, it prints the value 1. However, suppose you edit OnlyAB as follows and compile this
interface and not PrintA:
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;
11-9
Chapter 11
Completion and switch Expressions
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
};
// ...
}
11-10
12
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.
12-1
13
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:
13-1
Chapter 13
In JDK 11 and later, you can declare each formal parameter of an implicitly typed lambda
expression with the var identifier:
13-2
14
Safe Casting with instanceof and switch
You can use the instanceof operator to check at run time whether a cast from one type to
another is safe. A cast or conversion is safe if there's no loss of information or exception
thrown during the cast or conversion. You can use the instanceof operator to test whether a
cast is safe between two reference types or two primitive types. In addition, conversion safety
affects how switch statements and expressions behave.
Note:
The ability to use the instanceof operator to test whether a cast is safe between two
primitive types is a preview feature. A preview feature is a feature whose design,
specification, and implementation are complete, but is not permanent. A preview
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.
See JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview) for
additional information.
The following example uses instanceof as the type comparison operator to test whether the
Shape s is a Circle. If it is, then it's safe to cast Shape s to a Circle, which means that there's
definitely a radius that you can print:
The Java runtime throws a ClassCastException if it can't cast a reference type to another
reference type. If you remove the s instanceof Circle expression from the example, the
Java runtime throws an exception when it tries to cast the Shape, which is a Rectangle, to a
Circle:
14-1
Chapter 14
Conversion Safety and switch
When it comes to casting between primitive types, the Java runtime won't ever throw a
ClassCastException. It will convert the value (if the types are compatible), which might result
in the loss of information about the value's overall magnitude as well as its precision and
range. The following example casts an int to a byte:
int i = 2345323;
byte b = (byte)i;
System.out.println(b);
107
An improperly or unintendedly converted value, such as from 2345323 to 107, or more subtly,
from 2.05f to 2, can silently propagate through a program, causing potentially elusive bugs.
As the primitive types int and byte differ by their ranges, you could test that before casting the
value:
However, a clearer and more concise way to test whether a cast between two primitive type is
safe is to use instanceof as the type comparison operator:
if (i instanceof byte) {
System.out.println((byte)i);
}
Note that this example still has to cast the value i to a byte. The instanceof type comparison
operator only checks if i is a byte. It doesn't perform the actual cast.
This also works with instanceof as the pattern match operator, which has the added benefit of
performing the cast for you if the test is successful:
if (i instanceof byte b) {
System.out.println(b);
}
float f = 100.01f;
switch (f) {
// error: the switch statement does not cover
// all possible input values
case int i ->
System.out.println(i + " as an int ");
14-2
Chapter 14
Conversion Safety and switch
switch (f) {
case double d ->
System.out.println(d + " as a double");
}
The first switch statement isn't exhaustive because it only processes int values. The case
label case int i converts the float value of the switch statement's selector expression to an
int value. This conversion is not safe, which means that there's a possibility that information
may be lost about the value's overall magnitude as well as its precision and range. If this
switch statement were permitted, then an improperly or unintendedly converted value, such as
from 100.01f to 100, can propagate through a program, causing subtle bugs. To help prevent
this from happening, the compiler generates an error.
The second switch statement is exhaustive. The label double d converts the selector
expression's float to a double. This is an unconditionally exact conversion, which means that,
at compile time, it's known that the conversion from the first type to the second type is
guaranteed not to lose information or throw an exception for any value. Examples of
unconditionally exact conversions include converting from byte to int, from byte to Byte, from
int to long, and from String to Object.
Whether a conversion between primitive types is unconditionally exact also affects how
patterns can dominate other patterns. (See Pattern Label Dominance.) In the following
example, the compiler generates an error for the type pattern byte b:
switch (f) {
case int i ->
System.out.println(i + " as an int");
// error: this case label is dominated by a preceding case label
case byte b ->
System.out.println(b + " as a byte");
default ->
System.out.println(f + " as a float");
}
The type pattern int i dominates the type pattern byte b because converting from byte to
int is unconditionally exact.
14-3