Java Evolution: From Java 8 to Java 17, Compared with Kotlin and Clojure

Jorge Tovar - Sep 21 '23 - - Dev Community

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

GitHub

Code example

Repository

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;
Enter fullscreen mode Exit fullscreen mode

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;
    };
}
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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"}
        """
Enter fullscreen mode Exit fullscreen mode

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);
    }
};
Enter fullscreen mode Exit fullscreen mode

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player