Differences Between Java 11 and 17

Java 11 vs Java 17

Java 11 and Java 17 are two recent versions of the Java programming language, released in 2018 and 2021, respectively. While Java 11 introduced several new features and improvements, Java 17 builds upon those enhancements and introduces several new ones. Here’s a comparison of some of the key differences between Java 11 and Java 17.

Records

Java 14 introduced records as a preview feature, and Java 16 made them a standard feature. Records are a new kind of class that are designed to represent simple data containers. They provide a concise way to declare a class and its properties without the boilerplate of constructors, getters, and setters.

Here’s an example of using records in Java 17:

public record Point(int x, int y) { }

Point p = new Point(3, 4);
System.out.println(p.x() + ", " + p.y());

In this example, we’re using the record keyword to define a Point class that has two properties: x and y. We’re then creating a new Point object and printing its x and y values.

Switch Expressions

Java 12 introduced switch expressions as a preview feature, and Java 14 made them a standard feature. Switch expressions provide a more concise and expressive way to write switch statements.

Here’s an example of using switch expressions in Java 17:

int day = 3;
String dayName = switch (day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    case 4 -> "Thursday";
    case 5 -> "Friday";
    case 6 -> "Saturday";
    case 7 -> "Sunday";
    default -> throw new IllegalArgumentException("Invalid day number: " + day);
};

System.out.println("Day name: " + dayName);

In this example, we’re using a switch expression to convert a day number into a day name. The switch expression is more concise and easier to read than a traditional switch statement.

Text Blocks

Java 13 introduced text blocks as a preview feature, and Java 15 made them a standard feature. Text blocks provide a way to write multi-line string literals with minimal escaping and formatting.

Here’s an example of using text blocks in Java 17:

String html = """
                <html>
                    <body>
                        <h1>Hello, world!</h1>
                    </body>
                </html>
            """;

System.out.println(html);

In this example, we’re using a text block to define an HTML document. The text block allows us to write the document as it would appear in a text editor, with minimal escaping and formatting.

Records with Local Enums

Java 17 introduced a new feature that allows local enums to be used in records. This allows you to define an enum that’s specific to a record, without having to define it outside of the record.

Here’s an example of using local enums in records in Java 17:

public record Person(String name, int age, enum Gender { MALE, FEMALE } gender) { }

Person p = new Person("Alice", 25, Person.Gender.FEMALE);
System.out.println(p.name() + ", " + p.age() + ", " + p.gender());

In this example, we’re using a local enum to define the Gender property of a Person record. The local enum is defined inside the record, making it

specific to that record. We’re then creating a new Person object and printing its name, age, and gender properties.

instanceof Pattern Matching

Java 16 introduced instanceof pattern matching, which provides a more concise and expressive way to check the type of an object.

Here’s an example of using instanceof pattern matching in Java 17:

Object obj = "Hello, world!";
if (obj instanceof String s && s.length() > 5) {
    System.out.println("The string is longer than 5 characters.");
}

In this example, we’re using instanceof pattern matching to check if an object is an instance of a String and has a length greater than 5. The instanceof pattern matching syntax allows us to check the type of an object and extract its value in a single statement.

Local Variable Type Inference

Java 11 introduced local variable type inference, which allows you to declare local variables without specifying their type.

Here’s an example of using local variable type inference in Java 17:

var message = "Hello, world!";
System.out.println(message);

In this example, we’re using var to declare a local variable named message and assigning it a value of "Hello, world!". The compiler infers the type of the variable based on the value that’s assigned to it.

Final words

Java 17 introduces several new features and enhancements that build upon the improvements introduced in Java 11. These features, including records, switch expressions, text blocks, and local enums in records, make it easier and more expressive to write Java code. By keeping up with the latest versions of Java, developers can take advantage of these new features to write more efficient and maintainable code.

You may also like...

4 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x