Java 12,13,14,15,16 new features

Java 12,13,14,15,16 new features

Let's briefly go through some of the key features introduced in Java versions 12, 13, 14, 15, and 16, along with examples where applicable.

Java 12:

  1. Switch Expressions (JEP 325):

    • Enhances the switch statement to be more expressive.
    // Before Java 12
    String typeOfDay = "";
    switch (dayOfWeek) {
        case "Monday":
        case "Wednesday":
        case "Friday":
            typeOfDay = "Workday";
            break;
        case "Saturday":
        case "Sunday":
            typeOfDay = "Weekend";
            break;
        default:
            typeOfDay = "Invalid day";
            break;
    }

    // With Java 12
    String typeOfDay = switch (dayOfWeek) {
        case "Monday", "Wednesday", "Friday" -> "Workday";
        case "Saturday", "Sunday" -> "Weekend";
        default -> "Invalid day";
    };

Java 13:

  1. Text Blocks (JEP 355):

    • Introduces a new type of literal for multiline strings.
    // Before Java 13
    String html = "<html>\n" +
                  "  <body>\n" +
                  "    <p>Hello, Java 13!</p>\n" +
                  "  </body>\n" +
                  "</html>";

    // With Java 13
    String html = """
                  <html>
                    <body>
                      <p>Hello, Java 13!</p>
                    </body>
                  </html>
                  """;

Java 14:

  1. Switch Expressions (Standard Feature):

    • Switch expressions were made a standard feature in Java 14.
    // Java 14
    String typeOfDay = switch (dayOfWeek) {
        case "Monday", "Wednesday", "Friday" -> "Workday";
        case "Saturday", "Sunday" -> "Weekend";
        default -> "Invalid day";
    };
  1. Pattern Matching for instanceof (JEP 305):

    • Simplifies the use of instanceof and type casting.
    // Before Java 14
    if (obj instanceof String) {
        String s = (String) obj;
        // use 's'
    }

    // With Java 14
    if (obj instanceof String s) {
        // use 's'
    }

Java 15:

  1. Text Blocks (Standard Feature):

    • Text blocks were made a standard feature in Java 15.
    String query = """
                   SELECT *
                   FROM employees
                   WHERE department = 'IT'
                   ORDER BY salary DESC
                   """;

Java 16:

  1. JEP 376: ZGC: Concurrent Thread-Stack Processing:

    • Enhances the Z Garbage Collector (ZGC) with concurrent thread-stack processing.
  2. JEP 382: New macOS Rendering Pipeline:

    • Introduces a new rendering pipeline for macOS.
  3. JEP 383: Foreign-Memory Access API (Second Incubator):

    • Continues to enhance the Foreign-Memory Access API as an experimental feature.
    // Example using Foreign-Memory Access API
    try (MemorySegment segment = MemorySegment.allocateNative(1024)) {
        // Perform operations on the native memory segment
    }

These are just a few highlights of the features introduced in Java versions 12 through 16. Each release also includes various improvements, bug fixes, and other enhancements.