Java 21 brings exciting news for all developers 🎉 .
Java remains a fantastic language, and the speed at which new features are released is outstanding.
I have a repository that showcases Java's evolution from version 8 to the latest Java 17, complete with comparisons to Kotlin and Clojure. I'm currently working on updating it with Java 21's promising new features.
Explore these new features, especially if you're still using Java 8, and see how they're implemented in different languages. 📚
Table of Contents
- Local-Variable Syntax for Lambda Parameters
- Pattern Matching
- Records
- Sealed Classes
- Text Blocks
- Switch Expressions
GitHub
Local-Variable Syntax for Lambda Parameters
In Java, starting from version 11, you can use the var
keyword for lambda parameters, simplifying lambda expressions by allowing type inference. Here's an example:
// Java 11 Lambda Expression with var
(var a, var b) -> a + b;
Pattern Matching
Java introduced pattern matching in later versions, enhancing the way you work with instanceof
checks and type casting. It provides more concise and readable code. Here's a simplified example:
// Pattern Matching in Java
public static boolean available(Book book) {
return switch (book) {
case FictionBook fictionBook -> fictionBook.available();
case ProgrammingBook programmingBook -> programmingBook.exists();
case PsychologicalBook psychologicalBook -> psychologicalBook.imAAvailable();
case null, default -> false;
};
}
Records
Records, introduced in Java 16, provide a compact way to declare classes that are simple data carriers. They automatically generate methods like equals()
, hashCode()
, and toString()
. Example:
// Java Record
public record BookResponse(String json, Double version) {
}
BookResponse bookResponse = new BookResponse("{}", 2d);
System.out.println(bookResponse); // Automatically calls toString()
Sealed Classes
Java introduced sealed classes to restrict which other classes or interfaces may extend or implement them. It helps in defining a finite set of subclasses. Example:
// Sealed Class in Java
public sealed class Book permits FictionBook, ProgrammingBook, PsychologicalBook
Text Blocks
Text Blocks, available from Java 13 onwards, simplify working with multi-line strings. They allow you to write formatted text with minimal escape sequences:
// Java Text Block
json += """
{"title":"%s",
"author":"%s",
"pages":%s,
"karma":%s,
"eBook":%s,
"rate":%s,
"category":"%s"}
"""
Switch Expressions
Java introduced switch expressions in Java 12. They allow you to use a switch statement as an expression, making code more concise and readable:
// Java Switch Expression
var book = switch (category) {
case fiction -> {
karma = 25;
yield new FictionBook(title, author);
}
case programming -> {
karma = 40;
yield new ProgrammingBook(title, author);
}
case psychological -> {
karma = 30;
yield new PsychologicalBook(title, author);
}
};
These features have been introduced to make Java code more expressive and readable while reducing boilerplate code. You can choose the appropriate feature based on your project's requirements and Java version.
Conclusion
While I believe that Java 17 and its new releases are undeniably impressive, I can't help but acknowledge that Kotlin offers a multitude of features that enhance the developer experience and make coding more enjoyable. Clojure, on the other hand, has been a revelation for me, highlighting that you don't necessarily need classes and object-oriented programming to elegantly solve complex business logic challenges. It's a reminder that diverse programming languages can offer unique perspectives and solutions.
Top comments (1)
I have created a pull request to give some improvements
JDK 8 code
JDK17/JDK21
The given part with sealed class can be improved a lot...
Also the handling of files, and the rates in the CSV file can be handled simpler than doing flowcontrol via exceptions (isNumeric??) also calculation of the average is astonishing easier in Java than in Kotlin...
Old stile reading files like this:
It's easier even with JDK8:
There is one big issue from my point of view.. using Spring Boot is fine but to converting into JSON manually, that can be done easier just by defining classes/records and create a list of them and Spring Boot will do the rest...If I correctly understood the closure one it does not even use spring boot...
Also using an very old version of spring boot (2.6.3).. 3.1.4 is most recent version apart from 3.2.0-M3..