New Features in Java 13
Java 13, released in September 2019, introduced several features and enhancements. Let us delve into Java 13 new features.
1. Java 13 Switch Expressions – JEP 354
The introduction of switch expressions occurred in the Java 12 release as a preview feature. In Java 13, the syntax remains largely unchanged, with the only notable difference being the replacement of the “break” keyword with “yield” for returning a value from the case statement.
Consider a simple snippet.
var result = switch (day) { case "Monday", "Tuesday", "Wednesday","Thursday", "Friday" -> yield "Weekday"; case "Saturday", "Sunday" -> yield "Weekend"; default -> "Invalid day."; }; return result;
2. Java 13 Text Blocks (JEP 355)
Java 13 introduces a new feature called text blocks, which simplifies the creation of multi-line strings. Text blocks use triple double quotes ("""
) to define the start and end of the block. This eliminates the need for escape characters and makes it easier to write and read HTML or any other multi-line content directly within the source code.
Here is a simple example demonstrating the usage of text blocks in Java 13. With text blocks, you can maintain the indentation and structure of the content, enhancing the overall readability of your code.
public class TextBlocksExample { public static void main(String[] args) { String htmlContent = """ <html> <body> <p>Hello, Java 13 Text Blocks!</p> </body> </html> """; System.out.println(htmlContent); } }
In this example, triple double-quotes ("""
) are used to denote the beginning and end of the text block. The indentation inside the text block is preserved, making it easier to format and read. The resulting string assigned to htmlContent
will include the multiline HTML content without the need for explicit concatenation operators or escape characters.
2.1 New methods
Java 13 introduced several enhancements to the String
class, including the following methods:
- stripIndent(): Removes incidental white spaces from the start and end of the string.
- translateEscapes(): Translates escape sequences as per the string syntax.
- formatted(): Similar to the `String` `format()` method, this method supports formatting in text block strings.
3. Java 13 Dynamic CDS Archives (JEP 350)
Java 13 introduced the Dynamic Class-Data Sharing (CDS) Archives as part of JDK Enhancement-Proposal (JEP) 350. Class-data sharing is a feature in Java that allows a set of classes to be pre-processed into a shared archive file, which can then be memory-mapped at runtime to reduce startup time. Before Java 13, creating these archives required running a separate tool during the installation process.
JEP 350 enhances this feature by introducing a mechanism for creating dynamic CDS archives at runtime. This means that the archive file can be generated and updated while the application is running, based on the classes loaded and used during its execution.
The dynamic CDS archive feature is particularly beneficial for long-running applications or applications with varying workloads, as it allows the system to adapt and optimize the shared archive based on the actual usage patterns. This can result in improved startup performance and reduced memory footprint.
3.1 Overview of how dynamic CDS archives work
- Archive Generation at Runtime: With dynamic CDS archives, the system can generate and update the shared archive file while the application is running. This is in contrast to the traditional approach, where the archive was pre-built during installation.
- Adaptive Optimization: The dynamic nature of CDS archives allows them to adapt to the application’s runtime behavior. The system can observe which classes are frequently used or loaded and optimize the archive accordingly.
- Reduced Startup Time: By creating and updating the archive based on actual usage patterns, dynamic CDS archives aim to further reduce the startup time of Java applications.
- Improved Memory Usage: The shared archive is memory-mapped, allowing multiple Java processes to share the same set of classes in memory. This can lead to improved memory usage and a more efficient utilization of system resources.
It’s important to note that JEP 350 is part of the ongoing efforts to enhance Java’s performance and startup characteristics, and it aligns with the broader goal of making Java more efficient for various deployment scenarios.
4. ZGC: Uncommit Unused Memory (JEP 351)
JEP 351, titled “ZGC: Uncommit Unused Memory,” was introduced in Java 13 as part of the ongoing efforts to enhance the Z Garbage Collector (ZGC). This feature aims to improve the memory management efficiency of the ZGC by allowing it to uncommit memory that is no longer in use by the application.
Here are the key points of JEP 351:
- Dynamic Uncommit: The primary goal of JEP 351 is to introduce the capability for ZGC to dynamically uncommit memory that is no longer needed by the application. Uncommitting memory means releasing it back to the operating system, reducing the overall memory footprint of the Java process.
- Reduced Footprint: This feature is particularly beneficial for applications with varying memory usage patterns. By uncommitting memory that is identified as unused, the ZGC can reduce the overall memory footprint of the Java process. This is crucial for scenarios where memory resources are limited or need to be optimized.
- Adaptive Memory Management: The ability to uncommit memory dynamically allows ZGC to adapt to the changing memory requirements of the application. As the application’s memory needs evolve, ZGC can release unused memory back to the system, contributing to better resource utilization.
- Improved Efficiency: Uncommitting unused memory contributes to the efficient management of memory resources, aligning with the goals of the Z Garbage Collector to provide low-latency garbage collection with minimal impact on application responsiveness.
It’s important to note that JEP 351 is part of the ongoing improvements to the ZGC, which is designed to offer low-latency garbage collection for large heaps. The dynamic uncommit feature adds a level of adaptability to ZGC, allowing it to release memory back to the system in response to the application’s runtime behavior. This can be especially advantageous in cloud environments and scenarios where optimizing resource utilization is critical.
5. Reimplement the Legacy Socket API (JEP 353)
Java 13 introduced a significant improvement with the implementation of JEP 353: Reimplement the Legacy Socket API. This JEP aimed to modernize and enhance the existing Socket API, a foundational component of Java since its early versions.
The legacy Socket API, while functional, had accumulated technical debt and was deemed in need of a more contemporary and maintainable design. The reimplementation introduced in Java 13 addressed these concerns, bringing several benefits to Java developers.
5.1 Key Objectives and Features
- Modernization: The reimplementation focused on updating the Socket API to align with modern programming practices and to facilitate easier maintenance.
- Improved Performance: The updated Socket API aimed to enhance performance, ensuring that it meets the requirements of modern networking applications.
- Compatibility: While introducing changes, the JEP took into consideration maintaining compatibility with existing codebases to ease the transition for developers.
- Enhanced Functionality: The reimplementation provided an opportunity to introduce new features and improvements to the Socket API, addressing the limitations of the legacy version.
5.2 Impact on Java Development
The completion of JEP 353 marked a step forward in ensuring that Java’s networking capabilities remain robust and up-to-date. Developers now have access to a Socket API that not only preserves the reliability of the legacy version but also incorporates modern design principles and optimizations.
This enhancement underscores Java’s commitment to evolving with the ever-changing landscape of software development, providing developers with tools that are both efficient and contemporary.
6. Conclusion
Java 13, released in September 2019, brought forth incremental improvements and features aimed at refining the Java programming experience. Notable additions include text blocks (JEP 355), offering a more intuitive way to handle multiline strings by preserving formatting and reducing the reliance on escape characters. The introduction of Dynamic Class-Data Sharing (CDS) Archives (JEP 350) allowed for the dynamic generation and updating of shared archives at runtime, contributing to more adaptive and memory-efficient Java applications. The Z Garbage Collector (ZGC) received enhancements through JEP 351, enabling the dynamic uncommitting of unused memory to further reduce the memory footprint of applications. Additionally, the reimplementation of the legacy Socket API (JEP 353) aimed to modernize this fundamental component, aligning it with contemporary programming practices. Java 13 also introduced improvements to the String class, offering methods like stripIndent()
, translateEscapes()
, and formatted()
to simplify string manipulation and formatting in text block strings. While not revolutionary, Java 13 showcased the community’s commitment to continuous refinement, addressing specific pain points and enhancing the language’s readability, efficiency, and adaptability to modern programming paradigms. As the Java ecosystem evolves, staying abreast of the latest releases ensures that developers can leverage the newest features and improvements for their projects.