Differences Between Java 11 and Java?21

Differences Between Java 11 and Java?21

Java, a versatile and widely used programming language, has undergone significant changes and improvements over the years. Java 11 and Java 21 represent two different points in this evolution. As a Quality Assurance (QA) engineer, understanding the differences between these versions is vital for effective testing and ensuring the stability and reliability of software products. In this article, we’ll delve into specific examples of the differences between Java 11 and Java 21.

1. Pattern?Matching

Java 11 Example:

if (object instanceof String) {
    String str = (String) object;
    // Use str
}        

In Java 11, when checking the type of an object, you need to cast it explicitly to access its properties or methods.

Java 21 Example:

if (object instanceof String str) {
    // Use str directly
}        

Java 21 introduces pattern matching for the instanceof operator, allowing you to bind the object to a variable (str in this case) within the if statement. This improves code readability and reduces boilerplate code.

2. Record?Types

Java 11 Example:

class Point {
    private int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    // Getter and setter methods
}        

In Java 11, when defining simple data classes like Point, you need to write boilerplate code for constructors, getters, and setters.

Java 21 Example:

record Point(int x, int y) {}        

Java 21 introduces record types, which simplify the creation of data classes. With a single line of code, you can declare a record type with automatically generated constructor, accessor methods, equals(), hashCode(), and toString() methods.

3. Sealed?Classes

Java 11 Example:

public abstract class Shape {
    // ...
}

public final class Circle extends Shape {
    // ...
}        

In Java 11, you can’t explicitly control which classes can extend your abstract class, potentially leading to unforeseen issues.

Java 21 Example:

public abstract sealed class Shape permits Circle, Rectangle {
    // ...
}

public final class Circle extends Shape {
    // ...
}        

Java 21 introduces sealed classes, allowing you to specify which classes can extend or implement your class or interface. This provides better control over your code’s inheritance hierarchy and helps prevent unexpected subclassing.

4. Switch Expressions

Java 11 Example:

String day = "Monday";
String typeOfDay;

switch (day) {
    case "Monday":
    case "Tuesday":
        typeOfDay = "Weekday";
        break;
    default:
        typeOfDay = "Weekend";
        break;
}        

In Java 11, switch statements are limited to single-value cases, and you need to use explicit break statements.

Java 21 Example:

String day = "Monday";
String typeOfDay = switch (day) {
    case "Monday", "Tuesday" -> "Weekday";
    default -> "Weekend";
};        

Java 21 introduces enhanced switch expressions, allowing you to simplify switch statements by using arrow (->) syntax and eliminating the need for break statements.


As a QA engineer, understanding the differences between Java 11 and Java 21 is essential for effective testing. These examples highlight just a few of the notable improvements in Java 21, including pattern matching, record types, sealed classes, and enhanced switch expressions. By staying informed and adapting your testing strategies to leverage these new features, you can contribute to the quality and efficiency of software development projects that rely on the Java platform.


https://hakdogan26.medium.com/



要查看或添加评论,请登录

Hasan A.的更多文章

社区洞察

其他会员也浏览了