New Features in Java 22

New Features in Java 22

Hi. In this first post for the JAVA community, I’d like to delve into the exciting new features introduced in Java version 22. In this article, we'll discuss about 3 of them. You can find the complete list of all anticipated updates for version 22 at this link -->https://openjdk.org/projects/jdk/22/

Java remains a dominant force in the programming world, and version 22 arrives with significant innovations aimed at further optimizing software development. In this article, we will explore three new features of Java 22: String Templates, Unnamed Variables and Patterns, and the Class-File API, providing a detailed analysis with practical examples for each.

String Templates – JEP 459

In Java 22, String Templates introduce a new way to construct strings cleaner and more expressively. This functionality is particularly useful for generating dynamic strings that depend on variables and complex expressions.

Example 1: Basic Formatting

String name = "Andre";
String message = STR."Hi, \{nome}! Welcome to the 22 version.";
System.out.println(message);

In line 2, we create another String containing a message concatenated with the variable name. The command "STR." tells JAVA that it should process the entire expression as a String template.

This example demonstrates how the String Templates proposal in Java allows for the direct insertion of variables into strings, facilitating the construction of dynamic strings without the need for concatenation or String.format().

Note on Current Status

It is important to note that, up to the latest stable versions of Java (Java 17), string interpolation functionality as described has not yet been officially implemented and is under discussion for future versions. Therefore, these examples are based on the proposal and may not reflect functionalities currently available in Java.

If you are preparing content for publication or use in production environments, it is recommended to check the latest Java release notes or official documents for updates on these and other new features.

Unnamed Variables and Patterns – JEP 456

We often define temporary or standard variables that remain unused in the code. Most of the time, this is due to language restrictions, and removing them is prohibited or introduces side effects:

List<String> names= List.of("Andre","Charlie","David");
words.forEach(n -> System.out.println("We have a new JAVA version!"));
try{
    int number = Integer.parseInt("10b");
}catch (NumberFormatException exception){
    System.out.println("The parameter is not a number.");
}
switch (obj) {
    case Integer i -> System.out.println("Is an integer");
    case Float f -> System.out.println("Is a float");
    case String s -> System.out.println("Is a String");
    default -> System.out.println("Default message");
}

Unnamed variables (_) are perfect in such scenarios and make the variable's intent unequivocal. They cannot be passed in code or used or assigned values. Let's rewrite the previous examples, now using this new technique:

List<String> names= List.of("Andre","Charlie","David");
words.forEach(_ -> System.out.println("We have a new JAVA version!"));
try{
    int number = Integer.parseInt("10b");
}catch (NumberFormatException _){
    System.out.println("The parameter is not a number.");
}
switch (obj) {
    case Integer _ -> System.out.println("Is an integer");
    case Float _ -> System.out.println("Is a float");
    case String _ -> System.out.println("Is a String");
    default -> System.out.println("Default message");
}

Class File API – JEP 457

Which JAVA developer has never needed to run multiple Java files via the command line? Many never even considered this idea because it was a cumbersome and "boring" job, and everyone resorted to an IDE. But now with version 22 of JAVA, this job has become much more practical and easy.Class File API – JEP 457

Which JAVA developer has never needed to run multiple Java files via the command line? Many never even considered this idea because it was a cumbersome and "boring" job, and everyone resorted to an IDE. But now with version 22 of JAVA, this job has become much more practical and easy.

javac MyClass.java
java Myclass
Hello World!

//after JAVA 11 you can use this way
java MyClass.java
Hello World!

In cases where we had more than one class inside MyClass, we had to compile one by one. But with this new feature of version 22, JAVA does this for us automatically, we can just pass our "main" class and it takes care of compiling the necessary files for the correct execution.

public class Brazil{
    public void hiFromBrazil(){
        System.out.println("Hi from Brazil!");
    }
}
public class Argentina{
    public void hiFromArgentina(){
        System.out.println("Hi from Argentina!");
    }
}

Now, in our main class, let's instantiate the two objects and call the methods:

public class Countries{
    public static void main (String [] args){
        Brazil brazil = new Brazil();
        Argentina argentina = new Argentina();
        brazil.hiFromBrazil();
        argentina.hiFromArgentina();
    }
}

Executing by the command line:

java Countries.java
Hi from Brazil!
Hi from Argentina!

The innovations introduced in Java 22 are a testament to the platform's ongoing commitment to innovation and efficiency. Java 22 brings a multitude of updates, improvements, and new feature previews to Java.