top of page
Search

New Features Introduced in Java 13

  • schick09
  • Jan 20
  • 3 min read


Java 13, released in September 2019, brought several enhancements to the language and the JVM (Java Virtual Machine). While it wasn’t a long-term support (LTS) release, it introduced features aimed at improving developer productivity and the performance of the platform. Let’s dive into the key features and changes in Java 13 with some examples to illustrate their usage.


1. Text Blocks (Preview Feature)

Text blocks simplify the representation of multi-line string literals, eliminating the need for cumbersome escape sequences and concatenations. This feature is particularly helpful for embedding JSON, SQL, or HTML code directly in Java programs.


Syntax Example:

public class TextBlockExample {
    public static void main(String[] args) {
        String json = """
                {
                    "name": "John Doe",
                    "age": 30,
                    "city": "New York"
                }
                """;

        System.out.println(json);
    }
}

Key Points:

  • The opening delimiter (""") must appear on a new line.

  • Leading and trailing spaces can be managed to preserve the intended formatting.



2. Switch Expressions (Preview Feature)

Switch expressions were further enhanced in Java 13, making the switch statement more concise and expressive. This feature allows using switch as an expression and not just a statement, enabling direct assignment of values.


Syntax Example:

public class SwitchExpressionExample {
    public static void main(String[] args) {
        String day = "MONDAY";

        int numberOfLetters = switch (day) {
            case "MONDAY", "FRIDAY", "SUNDAY" -> 6;
            case "TUESDAY" -> 7;
            case "WEDNESDAY" -> 9;
            case "THURSDAY", "SATURDAY" -> 8;
            default -> throw new IllegalArgumentException("Unknown day: " + day);
        };

        System.out.println("Number of letters in " + day + ": " + numberOfLetters);
    }
}

Key Points:

  • Arrow syntax (->) is used for cleaner code.

  • break statements are no longer required to prevent fall-through.

  • You can return values directly from the switch expression.



3. Dynamic CDS Archives

Class Data Sharing (CDS) improves startup time and reduces memory footprint by sharing class metadata across multiple JVMs. Java 13 introduced dynamic CDS archives, which enable the creation of CDS archives during application runtime.


How It Works:

Dynamic CDS archives allow developers to:

  1. Run the application with a special JVM flag to dump a CDS archive.

  2. Use the archive during subsequent launches to speed up startup.


Command-Line Usage:

# Run the application and create a dynamic CDS archive
java -Xshare:dump -XX:ArchiveClassesAtExit=app-cds.jsa -cp MyApp.jar MyApp

# Use the CDS archive to improve startup performance
java -Xshare:on -XX:SharedArchiveFile=app-cds.jsa -cp MyApp.jar MyApp


4. Reimplementation of the Legacy Socket API

Java 13 replaced the underlying implementation of the socket API with a more modern and maintainable version. While this change is internal and doesn’t affect the API’s behavior, it improves the maintainability and performance of network operations.


Key Points:

  • The change is transparent to developers.

  • Improved reliability and performance of socket-based communication.



5. ZGC Improvements

The Z Garbage Collector (ZGC) introduced in Java 11 continued to evolve in Java 13 with support for uncommitting unused memory. This enhancement is particularly useful for applications with fluctuating memory usage.


Key Benefits:

  • Reduced memory footprint by returning unused memory to the operating system.

  • Applicable for environments with high memory demands, such as microservices and cloud applications.


Enabling ZGC:

java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xmx4g MyApp


Conclusion

Java 13 introduced several incremental yet impactful features aimed at improving code readability, runtime performance, and developer efficiency. While many of these features were in the preview stage, they paved the way for more robust and concise programming paradigms in future Java releases. If you haven’t explored Java 13 yet, give these features a try and see how they can streamline your development process.


For more insights into Java programming, stay tuned to this blog!








 
 
 

Comentarios


Dave Schick Consulting

©2023 by Dave Schick Consulting. Proudly created with Wix.com

bottom of page