Java Embraces Kotlin’s Influence: A Look at JDK 21’s Evolution
Introduction
Java, one of the most popular programming languages, has always been known for its strict object-oriented approach. However, with the release of JDK 21, Java is undergoing a significant transformation that showcases its openness to embracing new ideas and designs. Inspired by the success of Kotlin, a language that was initially influenced by Java, the latest Java release is adopting features and syntax enhancements that streamline coding and improve developer productivity. This article delves into the changes Java is making and how it is copying some of Kotlin’s innovations.
Simplifying the Syntax
One of the notable changes in JDK 21 is the simplification of Java’s syntax, reducing the boilerplate code required for simple programs. A prime example is the “Hello World” program. Previously, creating a “Hello World” program in Java involved defining a class and a static main method. However, in JDK 21, this is streamlined to just the main method declaration. The elimination of the class declaration is a preview feature (JEP 445) and may become stable in JDK 22. This change reflects the influence of Kotlin’s concise syntax and the desire to reduce verbosity in Java.
Borrowing from Kotlin’s Features
Java is also adopting several features from Kotlin, which further exemplifies the impact of Kotlin’s design choices. Some of the features being integrated into Java include:
- Kotlin Data Class -> Java Records: Kotlin’s data class, known for its concise representation of immutable data, has its equivalent in Java called records. Records provide a compact syntax for defining classes primarily used to store data. By introducing records, Java aims to simplify the creation of simple data-holding classes and reduce the amount of code required to achieve immutability.
- Kotlin Sealed Class -> Java Sealed Class: Sealed classes, a popular feature in Kotlin that enables the creation of restricted class hierarchies, are now being adopted in Java. Sealed classes help enforce exhaustive handling of subclasses, providing better control over the allowed inheritance structure and facilitating safer code.
- Kotlin Coroutines -> Java Virtual Thread: Kotlin’s coroutines, which simplify asynchronous programming by allowing sequential code execution, are being emulated in Java through virtual threads. Virtual threads aim to provide a lightweight concurrency model that simplifies the creation and management of concurrent code, making it easier to write and maintain high-throughput applications.
- Kotlin var/val -> Java var: Kotlin’s concise syntax for variable declaration, differentiating between mutable (var) and immutable (val) variables, is being introduced in Java as well. The introduction of the ‘var’ keyword in Java enables developers to declare local variables with type inference, reducing verbosity and improving code readability.
Expanding JDK 21’s Feature Set
Beyond Kotlin’s influence, JDK 21 introduces a diverse set of features and improvements. Some notable additions include:
- Key Encapsulation Mechanism (KEM) API: The KEM API enhances encryption techniques by securing symmetric keys through public cryptography. This addition enables applications to utilize advanced encryption algorithms and introduces support for higher-level protocols and cryptographic schemes.
- Generational Shenandoah Garbage Collector: JDK 21 introduces a generational mode for the Shenandoah garbage collector, enhancing sustainable throughput, memory utilization, and load-spike resilience. This feature aims to reduce memory footprint, CPU usage, and garbage collection pauses, providing performance benefits for a wide range of workloads.
- Pattern Matching Enhancements: JDK 21 brings improvements to pattern matching, including record patterns and pattern matching for switch expressions and statements. These enhancements enable more concise and expressive code, simplifying data navigation and processing.
- Vector API and Generational ZGC: The JDK 21 also introduces the Vector API, which provides a hardware-neutral abstraction for vector operations. This API allows developers to write vectorized code that can take advantage of vector instructions available on modern processors, improving performance for computationally intensive tasks.
In addition, JDK 21 includes the Generational Z Garbage Collector (ZGC), which is now in its second generation. ZGC is a low-latency garbage collector that reduces pause times by performing garbage collection concurrently with the application. The second-generation ZGC introduces enhancements such as improved scalability and reduced memory overhead, making it suitable for larger heap sizes.
JDK 21 also brings several updates and improvements to existing APIs and libraries. These enhancements include updates to the HTTP/2 client, enhanced support for macOS and Windows platforms, updates to the Java Flight Recorder, and improvements to the JavaFX platform.
Overall, JDK 21 reflects Java’s evolution as a language that embraces innovation and adopts features and ideas from other languages like Kotlin. By simplifying syntax, borrowing from Kotlin’s features, and introducing new capabilities, Java continues to evolve to meet the needs of modern software development and improve developer productivity.
Conclusion
JDK 21 marks a significant milestone in Java’s evolution, demonstrating its openness to embracing new ideas and designs. Inspired by Kotlin’s success, Java is adopting features and syntax enhancements that streamline coding and improve developer productivity. With simplified syntax, the introduction of features like records and sealed classes, the emulation of coroutines through virtual threads, and the inclusion of various other improvements, Java is evolving to become more concise, expressive, and efficient.
By incorporating these changes, Java aims to attract developers and provide them with modern tools and features while maintaining backward compatibility. The release of JDK 21 showcases Java’s commitment to staying relevant in the rapidly evolving software development landscape. As Java continues to evolve and adapt, it remains a powerful and versatile language for building a wide range of applications.