Java 21 brings some exciting new features to the world of programming. In this article, we’ll explore these Java 21 features with practical examples to make your Java coding experience even better.
Table of Contents
Please download OpenJDK 21 and add it to the PATH environment variable before switching to Java 21.
Java 21 Features:
1. Pattern Matching for Switch
Java 21 brings a powerful feature called Pattern Matching for Switch. It simplifies switch statements, making them more concise and readable. Check out an example:
Before java 21
// Before Java 21
String response = "yes";
switch (response) {
case "yes":
case "yeah":
System.out.println("You said yes!");
break;
case "no":
case "nope":
System.out.println("You said no!");
break;
default:
System.out.println("Please choose.");
}
In Java 21, you can rewrite the code provided above as follows:
// Java 21 Pattern Matching for Switch
String response = "yes";
switch (response) {
case "yes", "yeah" -> System.out.println("You said yes!");
case "no", "nope" -> System.out.println("You said no!");
default -> System.out.println("Please choose.");
}
Explore more about Pattern Matching for Switch in the full article.
2. Unnamed Patterns and Variables
Java 21 introduces Unnamed Patterns and Variables, making your code more concise and expressive. Here is a short part to show you an example:
String userInput = "User Input";
try {
int number = Integer.parseInt(userInput);
// Use 'number'
} catch (NumberFormatException ex) {
System.out.println("Invalid input: " + userInput);
}
Now, with Java 21, the above code can be rewritten as follows
String userInput = "User Input";
try {
int number = Integer.parseInt(userInput);
// Use 'number'
} catch (NumberFormatException _) {
System.out.println("Invalid input: " + userInput);
}
In this updated version, we no longer use the ‘ex’ variable; instead, we’ve replaced it with an underscore (_). This simple change helps streamline the code and makes it more concise.
For a deep dive into this feature and more practical examples, visit the full article.
3. Unnamed Classes and Instance Main Methods
Java 21 introduces a fresh approach to defining classes and instance main methods right in your code. Let’s take a quick look at how this feature operates:
// Java 21 Examples of Classes Without Names and Main Methods Inside Instances
public class UnnamedClassesDemo {
void main(String[] args) {
System.out.println("Hello from an unnamed class!");
}
}
Explore more about unnamed classes and instance main methods in the full article.
4. String Templates in Java
Java 21 introduces String Templates, simplifying string concatenation. Take a look:
// Java (using String.format)
String name = "Sachin P";
String message = String.format("Welcome %s", Java);
In Java 21, you can create a message using this syntax:
String name = "Sachin P";
String message = STR."Welcome \{name}!";
Discover the power of string templates and practical examples in the full article.
5. Sequenced Collections in Java 21
Java 21 introduces Sequenced Collections, making it easier to work with ordered data. Here’s a glimpse:
List<Integer> list = new ArrayList<Integer>();
list.add(0);
list.add(1);
list.add(2);
// Fetch the first element (element at index 0)
int firstElement = list.get(0);
// Fetch the last element
int lastElement = list.get(list.size() - 1);
In Java 21, you can retrieve elements using the following code.
List<Integer> list = new ArrayList<Integer>();
list.add(0);
list.add(1);
list.add(2);
// Fetch the first element (element at index 0)
int firstElement = list.getFirst();
// Fetch the last element
int lastElement = list.getLast();
Learn more about SequencedCollection, SequencedSet and SequencedMap and explore practical examples in the full article.
To put it simply, Java 21 brings some exciting improvements to the language. Features like Unnamed Patterns and Variables, along with Pattern Matching for Switch, make coding easier and improve code readability. These enhancements make Java development more efficient and enjoyable. Java developers now have the tools to write cleaner and more expressive code, marking a significant step forward in the world of Java programming.
If you’re curious to explore more features and details about Java 21, I recommend checking out the official Java release notes available at this link: Java 21 Release Notes. These release notes provide comprehensive information about all the changes and enhancements introduced in Java 21.
Nice concept