Share E-Book

Java 25 and Beyond Modern Java Made Easy (Michael Inden) (z-library.sk, 1lib.sk, z-lib.sk)

Author Michael Inden

Java
Language English

Bring your Java skills up to date and discover the full diversity of modern Java! This book guides you step by step through the most important new features up to the current Java 25 LTS, while also drawing on proven highlights from earlier versions. It covers numerous language and syntax improvements, such as switch extensions, records, and text blocks. We also take a look at new API functions related to streams, strings, virtual threads, structured concurrency, and much more. One focus is on the increasingly popular pattern matching, especially in combination with record patterns. The book is rounded off with practical exercises and detailed sample solutions to help you consolidate what you have learned. What You Will Learn: Trace Java’s evolution up to version 25 LTS. Describe key language features like switch, records, and text blocks. Explore pattern matching and record patterns in depth. Assess enhancements in streams and stream gatherer. Apply virtual threads and structured concurrency. Use modern Java features in practical coding scenarios. Identify hidden gems from earlier Java releases. Who This Book is for: Readers already proficient in Java who seek a focused overview of key features introduced in versions 12 through 25 LTS. It targets both emerging developers and seasoned professionals looking to deepen or refresh their understanding for practical application in modern projects.

Format PDF
Size 16.9 MB
5
Views
0
Downloads
0.00
Total Donations
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
Java 25 and Beyond Modern Java Made Easy — Michael Inden
Page 2
Java 25 and Beyond
Page 3
Michael Inden Java 25 and B eyond Modern Java Made Easy
Page 4
Michael Inden Zurich, Zürich, Switzerland ISBN 979-8-8688-2384-8 ISBN 979-8-8688-2385-5 ( eBook) https://doi.org/10.1007/979-8-8688-2385-5 © Michael Inden 2026 This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other p hysical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names a re exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. This Apress imprint is published by the registered company APress M edia, LLC, part of S pringer Nature. The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A. If disposing of this product, please recycle the paper.
Page 5
Dedicated to my little princess, my sunshine, and my beloved daughter, Sophie. I am endlessly grateful that you are here.
Page 6
Preface First of all, I would like to thank you for choosing this book. Inside, you will find a wealth of information about the new features in the current Java version, 25 LTS (Long-Term Support), as well as all the significant features introduced in its predecessors, Java 2 1 LTS and Java 17 LTS. Additionally, an outlook on Java 26 is included, highlighting refinements and new features that offer a glimpse of the platform’s ongoing evolution. Target A udience This book is not intended for novice programmers, but rather for readers who already have a solid familiarity with Java and are seeking concise information on the most important new f eatures in Java versions 12 through 25 LTS, as well as an impression of what is coming in Java 26. This book is specifically aimed at t he following tar get groups: 1. On the one hand, these are dedicated hobby programmers and computer science students, but also young professionals who are proficient in Java and are interested in the new features in the current Java versions. 2. On the other hand, the book is intended for experienced software developers and architects who want to supplement or refresh their knowledge to be able to evaluate whether and, if so, for which requirements the new Java versions can be a profitable alternative for future projects. What Does This Book Teach? In addition to theoretical knowledge, this book provides readers with practical examples to deepen their understanding, enabling them to successfully master the transition to Java 17 LTS, Java 21 LTS, or even the most modern Java 25 LTS in their own projects. The transition is made easier by a variety of exercises, including sample solutions for the most essential features. vii
Page 7
viii Preface To keep the examples in the book as precise and elegant as possible, I use various features from Java 8 LTS, 9, 10, and 11 LTS. Therefore, it is helpful if you already have experience with these versions. For those who need a concise and sound refresher, Chap. 15 provides a crash course on some of the key features included in Java versions 8 LTS to 11 LTS to help you get started. Structure of This Book In the following, I will briefly introduce the topics covered in each chapter. I often categorize new features into syntax, APIs, and JVM, where JVM encompasses all topics related to t he JVM, the compiler, and other tooling. Chapter 1: Introduction The introduction prepares you for modern Java in versions 17 LTS, 21 LTS, and 25 LTS, providing an overview of what you can expect to find in this book. I also discuss programming style and highlight the requirements for using the latest J ava versions with build tools and IDEs. Chapter 2: Key Features in JDK 12 to 17 LTS This chapter provides a concise introduction to essential syntax changes, API extensions, and JVM improvements included in Java 17 LTS. First, we will focus on various changes to the Ja va syntax. These range from multi-line strings and so-called pattern matching in instanceof to significant enhancements i n switch and records as a highly compact notation for defining data container classes. There are various features in the APIs of the JDK. These include additions to the String class and the Stream API. Finally, we will look at changes to the JVM in the form of an improvement in processing and information enrichment in the case of NullPointerExceptions. Chapter 3: Key Features in Java 21 LTS at a Glance This chapter provides an overview of the enhancements in Java 21 LTS, allowing you to quickly familiarize yourself with the new features and then proceed to read the relevant subsequent chapters according to your interests. Otherwise, it is recommended that you read the chapters in order from start to finish. Chapter 4: Syntax Innovations in JDK 18 to 21 LTS Record patterns enable the breakdown of records into their components, allowing for easy access. Pattern matching also works in switch in modern Java and has been enhanced in terms of functionality . Finally, unnamed patterns and variables allow marking certain elements in a record pattern or a variable as unused with a single underscore (“_”). Chapter 5: API Extensions in JDK 18 to 21 LTS Java 21 LTS includes various API innovations. Two highlights are Sequenced Collections and Virtual Threads. Sequenced Collections provide a uniform, mature interface for processing collections from both the front and back. Virtual Threads simplify multithreading,
Page 8
Preface ix with a particular focus on scaling. Mainly, there are no longer any restrictions on the number of Virtual Threads, unlik e for platform threads. Chapter 6: JVM Innovations in JDK 18 to 21 LTS In this chapter, we will examine the Simple Web Server and explore improvements in specifying code snippets i n JavaDoc, among other topics. Finally, we will discuss t he feature Unnamed Classes and Instance Main Methods. Chapter 7: Exercises on the Features in JDK 18 to 21 LTS This chapter contains various exercises on the new features in JDK 18 to 21 LTS, along with brief descriptions of sample solutions. Chapter 8: New Features in Java 25 LTS at a Glance This chapter provides an overview of the enhancements in Java 25 LTS. This allows you to familiarize yourself quickly and, depending on your interests, continue reading in the relevant subsequent chapters. Otherwise, it is r ecommended that you simply read the chapters in order. Chapter 9: Syntax Innovations in JDK 22 to 25 LTS Java 25 LTS offers many exciting innovations, such as primitive types in patterns, instanceof, and switch, as well as module import declarations. In the area of record patterns in particular, unnamed variables and patterns provide greater clarity and a more concise notation. Additionally, flexible constructor bodies enable increased flexibility in initialization actions. Chapter 10: API Extensions in JDK 22 to 25 LTS The APIs were also expanded in Java 25 LTS, for example, with Structured Concurrency, an elegant way to describe multithreading at a higher level of abstraction. Although Structured Concurrency has already undergone several preview iterations, it remains in its fifth preview in Java 25 LTS. In the context o f multithreading and information exchange, Scoped Values can simplify the transfer of values between different parts of an application. Another fundamental change is the i ntroduction of Stream Gatherers, which allow the definition of custom intermediate operations for the Stream API. Chapter 11: JVM Innovations in JDK 22 to 25 LTS In the area of the JVM, there are Markdown Documentation Comments and Compact Source Files to make it easier to get started with Java. Chapter 12: Exercises on the New Features in JDK 22 to 25 LTS This chapter contains various exercises and sample solutions for the new features in JDK 2 2 to 25 LTS. Chapter 13: Outlook: What’s New in Java 26 Java 26 focuses less on eye- catching syntax changes and more on strengthening stability, security, and per- formance. By further restricting deep reflection, the language moves closer to ensuring that final truly means immutable. At runtime, applications benefit from improvements such as higher throughput in the G1 garbage collector and HTTP/3 support in the HTTP (Hypertext Transfer Protocol) client. At the same time, key language and API features continue to mature. Structured Concurrency and
Page 9
x Preface Lazy Constants advance further toward finalization, providing clearer concurrency models and more efficient initialization patterns. Ov erall, Java 26 lays a solid foundation for future releases. Chapter 14: Summary and Conclusion This chapter begins with some thoughts on migrating to the latest Java versions. It then briefly summarizes the topics related to the many new features presented, especially those from Java 25 LTS. Chapter 15: Essentials from Java 8 LTS to 11 LTS This chapter summarizes the key additions to Java versions 8 LTS to 11 LTS. This will help you understand the various new features in the more recent Java versions, especially if you are not yet familiar with Java 8 LTS, 9, 10, or 11 LTS. In addition to introducing functional programming with lambdas, we also focus on streams. These are significant new features in JDK 8 LTS for processing data. Chapter 15 concludes with a brief overview of the JShell and how to execute Java programs using Launch Single-File Source Code Programs (Direct Compilation). Source Code and Executable Programs To avoid exceeding the scope of this book, the listings sometimes only show excerpts from executable programs. On demand, essential passages are occasionally highlighted in bold for better understanding. The accompanying material is available for download on the website https://github.com/Apress/Java-25-and-Beyond. The project provided contains all essential source files and executable programs. In the text, the respective program name is given in small caps, for example, D ATETIME- EXAMPLE, so that it is more easily recognizable as an executable. Additionally, the project includes the files build.gradle and pom.xml, which describe the build processes for Gradle and Maven, respectively. Java Version(s) Used In Pa rt I, all examples are based on Java 17 LTS. P art II relies on Java 21 LTS, which contains numerous interesting enhancements. Finally, Part III describes the new features of Java 25 LTS. As an outlook Chap. 13 on novelties in Java 26 then covers state-of-the-art Java 26. After reading this book, you will be well-equipped for modern Java and be able to use the new features in your own experiments and hobby projects or (hopefully even) professionally. Conventions Fonts Used The following conventions apply to fonts in this book: In addition to the font used here, important passages are marked in italics or italics and bold. Names
Page 10
Preface xi of programs and design patterns are written in SMALL CAPS. Listings with source code are set in the font Courier to make it clear that this is an excerpt from a Java program. This font is also used for names of classes, methods, constants, and parameters in standard text. Practical Tips and Advice This book is peppered with various practical tips. These present interesting back- ground information or point out pitfalls. Boxes formatted in this way can be found throughout the book, providing valuable tips and additional information to supplement the main text. Classes Used from the JDK Whenever classes from the JDK are mentioned for the first time in the text, their fully qualified name, that is, including the package structure, is provided: the class String would therefore be noted as java.lang.String—all further mentions omit the package name. This rule makes it easier to find your way around and locate items in the JDK and also prevents the subsequent text from becoming too bloated. The fully qualified specification is particularly helpful, a s import statements are rarely shown in the listings. Method calls described in the text sometimes include the types of the passed parameters, such as substring(int, int). If the parameters are not crucial in a given context, they are often omitted for reasons of readability—this is especially true for methods with generic parameters. Abbreviations Used In this book, I use the abbreviations shown in Table 1. Other abbreviations are mentioned in parentheses after their first definition i n the text and then used as needed. Table 1 Abbrev iations used Abbreviation Meaning API Application Programming Interf ace ASCII American Standard Code for I nformation I nterchange (G)UI (Graphical) User I nterface IDE Integrated Development En vironment JDK Java Development K it JEP JDK Enhancement P roposal JLS Java Language Specification JRE Java Runtime En vironment JSR Java Specification R equest JVM Java Virtual Machine
Page 11
xii Preface Suggestions and Criticism Despite great care and multiple proofreading, ambiguous wording or even errors cannot be completely ruled out. If you notice anything of this nature, please do not hesitate to let me know. I am also happy to receive suggestions or ideas for improvement. P lease get in touch with me by email at: michael_inden@hotmail.com Zurich, Zürich, Switzerland Michael Inden February 2026 Acknowledgments Writing a book is a wonderful but laborious and time- consuming task. It is almost impossible to accomplish this alone. I want to thank everyone who contributed directly or indirectly to the success of this book. In particular, I w as able to rely on a strong team of proofreaders during the manuscript preparation. It is beneficial to gain insight from different perspectives and experiences. For the current Java 25 LTS version of this book, I was able to count on three proofreaders in particular: Sven Woltmann, Dr. Clemens Gugenberger, and Dennis Woithe. First of all, I would like to thank Sven, known for his excellent blog at https:// www.happycoders.eu/. Sven’s expertise has led to valuable improvements in t he content and language of many chapters. I would also like to extend my heartfelt thanks to Clemens for his helpful suggestions and insightful questions, which prompted me to think critically and improve specific passages—not to mention t he many minor corrections he pointed out. I would also like to thank Dennis, who read the manuscript with great attention to detail and uncovered even the most hidden potential for improvement. Additionally , he repeatedly offered suggestions on how to make t he examples even more concise. In the area of multithreading, Marwan Abu-Khalil supported me this time and contributed several improvements and suggestions—many t hanks for that and for our interesting discussions at JAX 2025 in Mainz. In the past, Michael Kulla, a well-known trainer for both Java SE and Jakarta EE, has been highly committed. I want to thank him for his thorough review of man y chapters and his insightful comments. In previous versions, Prof. Carsten Kern and Sven Friederichs also ensured greater linguistic consistency in various places and made different suggestions for improving the content. Thank you! Finally, I would like to thank Ursula Zimpfer for her eagle eyes during copyedit- ing. This allowed me to eliminate a few minor errors in previous (German) editions.
Page 12
Contents 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Release Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Overview of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.2.1 Part I: Key Features in Java 12 to 17 LTS . . . . . . . . . . . . . . . . 4 1.2.2 Part II: Key Features in Java 18 to 21 LTS . . . . . . . . . . . . . . . 5 1.2.3 Part III: New Features in Java 22 to 25 LTS . . . . . . . . . . . . . 6 1.3 Basic Structure of the Sample Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.4 Note on Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.4.1 Thoughts on Source Code Compactness . . . . . . . . . . . . . . . . . 8 1.4.2 Thoughts on final and var . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.4.3 Block Comments in Listings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.4.4 Thoughts on Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.5 Installing Java 25 LTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.5.1 Java Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.5.2 Installing the JDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.5.3 Post-Installation Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.5.4 Verifying Java Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.6 Configurations for Build Tools and IDEs for Java 25 LTS . . . . . . . . . 17 1.6.1 Java 25 LTS with Gradle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.6.2 Java 25 LTS with Maven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.6.3 Java 25 LTS with IntelliJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.6.4 Java 25 LTS with Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.7 Trying Out the Examples and Solutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.7.1 Trying Out New Java Features with the JShell or the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.7.2 Trying Out New Java Features in a Sandbox . . . . . . . . . . . . 26 xiii
Page 13
xiv Contents Part I Key Features in Java 12 to 17 L TS 2 Key Features in JDK 12 to 17 LTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.1 Text Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.1.1 Basic Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.1.2 Special Features and Conveniences . . . . . . . . . . . . . . . . . . . . . . 33 2.1.3 Escaping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2.1.4 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2.1.5 Placeholders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 2.2 Switch Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.2.1 Introductory Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.2.2 Completeness Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2.2.3 Pitfalls of the Old Syntax and Remedies . . . . . . . . . . . . . . . . . 43 2.2.4 Return with yield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 2.2.5 Backward-Compatible Specification for case with yield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 2.3 Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2.3.1 Introductory Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2.3.2 Records for DTOs and Return Values . . . . . . . . . . . . . . . . . . . . 52 2.3.3 Extension Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 2.3.4 Special Feature: Generics in Records . . . . . . . . . . . . . . . . . . . . 58 2.3.5 Special Feature: Records and Interfaces . . . . . . . . . . . . . . . . . 60 2.3.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 2.4 Records: Beyond the Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 2.4.1 The Influence of the Data Structure i n Which Records Are Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 2.4.2 Detecting Implementation Errors . . . . . . . . . . . . . . . . . . . . . . . . . 66 2.4.3 Records in the Context of the Builder Pattern . . . . . . . . . . . 67 2.4.4 Records and Immutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 2.4.5 Correction 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 2.4.6 Correction 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 2.5 Pattern Matching for instanceof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 2.5.1 New Syntax and Introductory Example . . . . . . . . . . . . . . . . . . 74 2.5.2 In Practical Use. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 2.6 Extension in the String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 2.7 Extensions in the Stream API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 2.7.1 The teeing() Collector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 2.7.2 The toList() Method as a Shortcut to the Collector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 2.7.3 The Intermediate Operation mapMulti() . . . . . . . . . . . . . . . 84 2.8 Improvements to NullPointerExceptions . . . . . . . . . . . . . . . . . . . . . . . 87 2.8.1 Introductory Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 2.8.2 Troubleshooting Complex Expressions . . . . . . . . . . . . . . . . . . 90
Page 14
Contents xv Part II Key Features in Java 18 to 21 L TS 3 Key Features in Java 21 LTS at a Glance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 3.1 JEPs in Java 21 LTS at a Glance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 4 Syntax Innovations in JDK 18 to 21 LTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 4.1 JEP 440: Record Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 4.1.1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 4.1.2 Nested Record Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 4.1.3 Type Inference for Generic Record Patterns . . . . . . . . . . . . . 107 4.1.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 4.2 JEP 441: Pattern Matching for switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 4.2.1 Simple Pattern Matching with Java 17 LTS (Preview) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 4.2.2 Adjustments to the Specification of C onditions (Guarded Patterns) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 4.2.3 Improvement of the Dominance Check . . . . . . . . . . . . . . . . . . 116 4.2.4 Special Case: Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 4.2.5 Improvement of Completeness Analysis . . . . . . . . . . . . . . . . . 119 4.2.6 Specifying Record Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 4.2.7 Qualified Enums. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 4.3 JEP 443: Unnamed Patterns and Variables (Preview) . . . . . . . . . . . . . . 128 4.3.1 Motivation for Unnamed Variables and Patterns . . . . . . . . 128 4.3.2 Remedy Using Unnamed Variables and Patterns . . . . . . . . 130 5 API Extensions in JDK 18 to 21 LTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 5.1 JEP 416: Reimplement Core Reflection with Method Handles . . . . 136 5.2 JEP 418: Internet-Address Resolution SPI . . . . . . . . . . . . . . . . . . . . . . . . . . 138 5.3 JEP 431: Sequenced Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 5.3.1 Remedy Sequenced Collections . . . . . . . . . . . . . . . . . . . . . . . . . . 142 5.3.2 A Look Behind the Scenes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 5.3.3 Sequenced Collections in Action . . . . . . . . . . . . . . . . . . . . . . . . . 146 5.4 JEP 444: Virtual Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 5.4.1 Introductory Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 5.4.2 Restrictions of Conventional Threads . . . . . . . . . . . . . . . . . . . . 153 5.4.3 Motivation for Virtual Threads in Practice . . . . . . . . . . . . . . . 155 5.4.4 Performance Comparison for Platform and Virtual Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 5.4.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 5.5 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 5.5.1 JEP 400: UTF-8 by Default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 5.5.2 JEP 452: Key Encapsulation Mechanism API . . . . . . . . . . . 163 5.5.3 New Features in the Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . 163 5.5.4 New Features When Creating a HashMap<> . . . . . . . . . . . . . 165 5.5.5 Various Changes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Page 15
xvi Contents 5.6 Deprecations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 5.6.1 JEP 421: Deprecate Finalization for Removal . . . . . . . . . . . 169 5.6.2 Further Deprecations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 6 JVM innovations in JDK 18 to 21 LTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 6.1 JEP 408: Simple Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 6.2 JEP 413: Code Snippets in Java API Documentation . . . . . . . . . . . . . . 175 6.3 JEP 445: Unnamed Classes and Instance Main Methods (Preview) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 6.3.1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 6.3.2 Simplification I: Instance main() . . . . . . . . . . . . . . . . . . . . . . . . 178 6.3.3 Simplification II: Unnamed Class . . . . . . . . . . . . . . . . . . . . . . . . 179 6.3.4 Further Possibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 6.3.5 Multiple main(): What Happens in the Background?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 6.3.6 Java on the Way to Script-Based Execution? . . . . . . . . . . . . 181 6.4 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 6.4.1 Linux/RISC-V Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 6.4.2 Generational ZGC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 6.4.3 Deprecate the Windows 32-bit x86 Port for Removal . . . 182 6.4.4 Prepare to Disallow the Dynamic Loading of Agents . . . 183 7 Exercises on the Features in JDK 18 to 21 LTS . . . . . . . . . . . . . . . . . . . . . . . . . 185 7.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 7.2 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Part III New Features in Java 22 to 25 L TS 8 New Features in Java 25 LTS at a Glance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 8.1 JEPs in Java 25 LTS at a Glance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 9 Syntax Innovations in JDK 22 to 25 LTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 9.1 JEP 456: Unnamed Variables and Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . 200 9.2 JEP 467: Markdown Documentation Comments . . . . . . . . . . . . . . . . . . . 201 9.2.1 Introductory Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 9.2.2 Brief Overview of Markdown Features . . . . . . . . . . . . . . . . . . 202 9.2.3 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 9.2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 9.3 JEP 507: Primitive Types in Patterns (Third Preview). . . . . . . . . . . . . . 207 9.3.1 Introductory Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 9.3.2 Special Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 9.3.3 Special Feature: Possibilities of Type Checking . . . . . . . . . 213 9.4 JEP 511: Module Import Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 9.4.1 New Feature: Module Import Declarations . . . . . . . . . . . . . . 217 9.4.2 Special Cases: Ambiguous Imports . . . . . . . . . . . . . . . . . . . . . . 221 9.4.3 Correction of the Sample Program . . . . . . . . . . . . . . . . . . . . . . . 222 9.4.4 Module Imports and the JShell . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Page 16
Contents xvii 9.5 JEP 513: Flexible Constructor Bodies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 9.5.1 Validation of Arguments for the B ase Class Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 9.5.2 Preparing Arguments for the Base Class Constructor . . . 227 9.5.3 Improvements in Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 9.5.4 Special Feature: Actions Before Calling this(). . . . . . . . 230 10 API Extensions in JDK 22 to 25 LTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 10.1 JEP 454: Foreign Function and Memory API. . . . . . . . . . . . . . . . . . . . . . . 234 10.2 JEP 471: Deprecate the Memory-Access Methods . . . . . . . . . . . . . . . . . 238 10.3 JEP 484: Class-File API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 10.4 JEP 485: Stream Gatherers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 10.4.1 Introduction and Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 10.4.2 Overview of Selected Predefined Gatherer Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 10.4.3 Practical Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 10.4.4 Combining Gatherers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 10.4.5 A Look at mapConcurrent(). . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 10.4.6 Potential Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 10.4.7 Custom Stream Gatherers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 10.4.8 State Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 10.4.9 Construction Methods in the Gatherer Interface . . . . . . . 262 10.4.10 Generalization Through Parameterization . . . . . . . . . . . . . . . 263 10.4.11 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 10.5 JEP 502: Stable Values (Preview) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 10.6 JEP 505: Structured Concurrency (Fifth Preview) . . . . . . . . . . . . . . . . . . 267 10.6.1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 10.6.2 Implementation with Structured Concurrency . . . . . . . . . . . 269 10.6.3 Joiner anySuccessfulResultOrThrow() . . . . . . . . . . . . . 274 10.6.4 Details on Predefined and Custom Joiners . . . . . . . . . . . . . . . 276 10.6.5 Nested Structured Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . 281 10.6.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 10.7 JEP 506: Scoped Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 10.7.1 Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 10.7.2 Value Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 10.7.3 Further Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 10.7.4 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 10.7.5 Technical Background and Conclusion . . . . . . . . . . . . . . . . . . 296 10.8 JEP 508: Vector API (Tenth Incubator) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 10.8.1 Introductory Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 10.8.2 Further Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 10.8.3 Implementation Using the Vector API . . . . . . . . . . . . . . . . . . . 300 10.8.4 Auto-vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 10.9 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Page 17
xviii Contents 11 JVM Innovations in JDK 22 to 25 LTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 11.1 JEP 423: Region Pinning for G1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 11.2 JEP 458: Launch Multi-File Source Code Programs . . . . . . . . . . . . . . . 308 11.3 JEP 474: ZGC: Generational Mode by Default . . . . . . . . . . . . . . . . . . . . . 311 11.4 JEP 512: Compact Source Files and Instance Main Methods . . . . . . 312 11.4.1 New Features in Java 22 to 25 LTS. . . . . . . . . . . . . . . . . . . . . . . 313 11.4.2 Further Innovations in Java 22 to 25 LTS . . . . . . . . . . . . . . . . 316 11.4.3 Step-by-Step Development of a Program . . . . . . . . . . . . . . . . 319 11.4.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 11.5 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 12 Exercises on the Features in JDK 22 to 25 LTS . . . . . . . . . . . . . . . . . . . . . . . . . 325 12.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 12.2 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Part IV Outlook and Summary 13 Outlook: What’s New in Java 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 13.1 JEP 500: Prepare to Make Final Mean Final . . . . . . . . . . . . . . . . . . . . . . . . 342 13.1.1 Introductory Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 13.1.2 New Features in Java 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 13.2 JEP 517: HTTP/3 for the HTTP Client API . . . . . . . . . . . . . . . . . . . . . . . . 345 13.2.1 Introduction to HTTP/2 API (Java 11 LTS) . . . . . . . . . . . . . . 346 13.2.2 New Features in Java 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 13.3 JEP 525: Structured Concurrency (Sixth Preview) . . . . . . . . . . . . . . . . . 348 13.3.1 New Features in Java 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 13.4 JEP 526: Lazy Constants (Second Preview) . . . . . . . . . . . . . . . . . . . . . . . . 351 13.4.1 New Features in Java 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 13.5 JEP 530: Primitive Types in Patterns, instanceof, and Switch (Fourth Preview) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 13.5.1 New Features in Java 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 13.6 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 13.7 Modifications Without JEPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 13.8 Conclusion on the New Features in Java 26 . . . . . . . . . . . . . . . . . . . . . . . . 361 14 Summary and Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 14.1 Thoughts on Migrating and Upgrading to Java 21 L TS or 25 LTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 14.2 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 14.3 Final Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366 14.3.1 Personal Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Page 18
Contents xix Part V A ppendix 15 Essentials from Java 8 LTS to 11 LTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 15.1 Getting Started with Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 15.1.1 Lambdas by Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 15.1.2 Functional Interfaces and SAM Types . . . . . . . . . . . . . . . . . . . 370 15.1.3 Type Inference and Short Forms of Syntax . . . . . . . . . . . . . . 372 15.1.4 Method References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 15.2 Streams at a Glance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 15.2.1 Create Streams: Create Operations . . . . . . . . . . . . . . . . . . . . . . . 376 15.2.2 Intermediate and Terminal Operations at a Glance . . . . . . 376 15.2.3 Intermediate Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 15.2.4 Terminal Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 15.3 Miscellaneous Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 15.3.1 Extensions in Interface Comparator<T> . . . . . . . . . . . . . . . . 382 15.3.2 The Class Optional<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 15.3.3 Syntax Extension var . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 15.4 Java + REPL => jshell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 15.4.1 Introductory Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 15.4.2 Additional Commands and Options . . . . . . . . . . . . . . . . . . . . . . 390 15.4.3 More Complex Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 15.5 Launch Single-File Source Code Programs (Direct Compilation). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 15.5.1 Introductory Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 15.5.2 Special Feature: Shebang Script . . . . . . . . . . . . . . . . . . . . . . . . . . 395 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Page 19
About the A uthor Michael Inden is a Java and Python enthusiast with over twenty-five years of professional experience. He has worked for various international compa- nies in multiple roles, including software devel- oper, software architect, team leader, CTO, Head of Development, trainer and freelance consultant. Currently, he works a lecturer in software engineer- ing. In addition, he speaks at conferences and writes specialist books, such as the pairings “Java Chal- lenges”/“Python Challenges”, as well as numerous books in German, including “Der Weg zum Java- Profi” and “Einfach Java”/“Einfach Python”. xxi
Page 20
About the Technical R eviewer Andres Sacco is a Technical Leader at TravelX and has experience with many languages, including Java, PHP, and Node.js. In his previous job, Andres helped identify alternative approaches to optimize data transfer between microservices, reducing infrastructure costs by 55%. Before introducing these optimizations, he investigated alternative testing methods to improve coverage beyond the microservice unit tests. Andres is a co-author of the books Beginning Scala 3 and Practical Spring LDAP, published by Apress. xxiii
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
Back to List