DEV Community

Cover image for Java Evolution: From Java 8 to Java 17, Compared with Kotlin and Clojure
Jorge Tovar
Jorge Tovar

Posted on • Edited on

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

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.

Top comments (1)

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise • Edited

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...

public sealed interface Book permits Book.FictionBook, Book.ProgrammingBook, Book.PsychologicalBook {
  String title();
  String author();

  record FictionBook(String title, String author) implements Book {
    public boolean available() {
      return false;
    }
  }
  record ProgrammingBook(String title, String author) implements Book {
    public boolean exists() {
      return true;
    }
  }
  record PsychologicalBook(String title, String author) implements Book {
    public boolean imAAvailable() {
      return false;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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:

            while ((csvLine = csvReader.readLine()) != null) {
                json = createJsonRecord(csvLine, json);
            }
Enter fullscreen mode Exit fullscreen mode

It's easier even with JDK8:

        try (var csvReader = Files.newBufferedReader(inputCsv)) {
            return csvReader
                .lines()
                .map(splitByComma)
                .map(Arrays::asList)
                .map(this::createJsonRecord)
                .collect(joining(",", "[", "]"));
       }...
Enter fullscreen mode Exit fullscreen mode

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..