This work is licensed under a Creative Commons Attribution 4.0 International License .
This book provides a concise guide to the key concepts and features introduced in Java 23. It is intended for developers who are already familiar with Java and want to learn about the latest advancements in the language.
Java 23 introduces a number of new features and enhancements, including:
Java 9 introduced the Java Platform Module System (JPMS) to address the challenges of managing dependencies in large applications. However, JPMS has not been as widely adopted as initially hoped. One of the reasons for this is that importing packages from modules can be verbose. For example, to import all of the public packages from the java.base
module, you would need to write the following code:
import java.lang.*;
import java.util.*;
import java.io.*;
// ... and so on
Java 23 introduces a new feature called Simplified Module Imports that allows you to import all of the public packages from a module with a single statement. For example, the following code imports all of the public packages from the java.base
module:
import module java.base;
This feature can make your code more concise and readable, especially if you are working with modules that export a large number of packages.
Java is a powerful and versatile language, but it can also be verbose, especially for beginners. To address this, Java 23 introduces two new features: Implicitly Declared Classes and Simplified Module Imports.
Implicitly Declared Classes allow you to omit the public class
declaration for simple programs that consist of a single file. For example, the following code is a valid Java 23 program:
void main() {
System.out.println("Hello, world!");
}
Simplified Module Imports, as described above, allow you to import all of the public packages from a module with a single statement.
Together, these two features can make Java more approachable for beginners and can help to reduce the amount of boilerplate code that you need to write.
Concurrent programming is a powerful technique that allows you to write programs that can do multiple things at the same time. However, concurrent programming can also be difficult to get right. Java 23 introduces a new feature called Structured Concurrency that aims to make concurrent programming easier and more reliable.
Structured Concurrency is based on the idea of structured programming, which is a programming paradigm that emphasizes the use of structured control flow constructs, such as loops and conditional statements. Structured Concurrency brings this same idea to concurrent programming by providing a way to structure concurrent tasks in a way that is easy to understand and reason about.
The key to Structured Concurrency is the StructuredTaskScope
class. This class provides a way to create a scope for a group of related tasks. When a task is created within a StructuredTaskScope
, the task is guaranteed to complete before the scope exits. This makes it easy to ensure that all of the tasks in a group have completed before proceeding with the rest of your program.
Java provides a number of ways to share data between different parts of your program. One common way to share data is to use thread-local variables. A thread-local variable is a variable that has a separate value for each thread. This can be useful for sharing data that is specific to a particular thread, such as a user's session information.
However, thread-local variables have a number of drawbacks. First, they can be difficult to manage, especially in large programs. Second, they can lead to memory leaks if they are not used carefully.
Java 23 introduces a new feature called Scoped Values that provides a more convenient and reliable way to share data between different parts of your program. A scoped value is a value that is associated with a particular scope. A scope can be a block of code, a method, or a class. When a scoped value is created, it is accessible to all of the code within the scope.
Scoped values have a number of advantages over thread-local variables. First, they are easier to manage because they are automatically scoped to a particular block of code. Second, they are less likely to lead to memory leaks because they are automatically garbage collected when the scope exits.
Java constructors are special methods that are used to initialize objects. When you create a new object, the constructor for the object's class is called. The constructor is responsible for initializing the object's fields to their default values.
In Java, you can call one constructor from another constructor using the this()
or super()
keywords. For example, the following code defines a class called Person
with two constructors:
public class Person {
private String name;
private int age;
public Person(String name) {
this(name, 0); // Call the other constructor with the given name and an age of 0
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
The first constructor, Person(String name)
, calls the second constructor, Person(String name, int age)
, passing in the given name and an age of 0.
In Java 23, you can now include statements before the call to this()
or super()
in a constructor. This can be useful for performing validation or initialization logic before the superclass constructor is called.
Java provides a way to write documentation comments in your code. These comments are used to generate API documentation for your code. API documentation is a set of documentation that describes the classes, methods, and fields in your code.
Java documentation comments are written using a special syntax that is based on HTML. For example, the following code shows a documentation comment for a method called calculateSum()
:
/**
* Calculates the sum of two integers.
*
* @param a The first integer.
* @param b The second integer.
* @return The sum of a
and b
.
*/
public int calculateSum(int a, int b) {
return a + b;
}
Java 23 introduces a new feature that allows you to write documentation comments using Markdown. Markdown is a lightweight markup language that is easier to read and write than HTML.
To write a documentation comment using Markdown, you can use the ///
delimiter instead of the /**
and */
delimiters. For example, the following code shows the same documentation comment for the calculateSum()
method, written using Markdown:
/// Calculates the sum of two integers.
///
/// @param a The first integer.
/// @param b The second integer.
/// @return The sum of `a` and `b`.
public int calculateSum(int a, int b) {
return a + b;
}
The Z Garbage Collector (ZGC) is a low-latency garbage collector that was introduced in Java 11. ZGC is designed for applications that require very low pause times, even for large heaps.
ZGC uses a generational garbage collection algorithm. This means that it divides the heap into different generations, and it collects objects in different generations differently. Young generation objects are objects that have been created recently. Old generation objects are objects that have been alive for a longer period.
In Java 23, ZGC is now enabled by default. This means that if you are using Java 23 or later, your application will automatically use ZGC unless you explicitly disable it.
Java 23 is a significant release that introduces a number of new features and enhancements. These new features and enhancements can help you to write more concise, readable, and maintainable code. If you are a Java developer, I encourage you to download Java 23 and try out the new features.