Java Versions and Features
In this post, I will cover Java’s Long-Term Support (LTS) versions and features.
Java 8
Java 8 was released in 2014.
Lambda expressions
Lambda expression is a block of code that you can pass around so it can be executed later, once or multiple times. It can be seen as a brief version of instantiating a anonymous class of a functional interface (only have one abstract method).
Common functional interfaces
- java.lang.Runnable
- java.util.function.Function
- java.util.function.Consumer
Example 1: Runnable interface
Using anonymous class
public static void main(String[] args) { |
Using lambda expression
public static void main(String[] args) { |
Example 2: Function interface
public static void printGreetWithDifferentCase(Function<String, String> caseConverter) { |
Using anonymous class
public static void main(String[] args) { |
Using lambda expression
public static void main(String[] args) { |
Interfaces with default methods
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.
For example,
public class Test0423 { |
Stream API
Streams and loops both operate on collections. But stream operations are more concise and powerful.
For example,
double average = users |
Optional Class
Reduces null checks and helps prevent NullPointerException.
String deptName = Optional.ofNullable(user) |
Java 11
Java 11 was released in 2018. In this section, cover Java 9~11 features.
Modules
Since Java 9
Java 9 introduced a new system, called the Java Platform Module System, that is supported by the Java compiler and virtual machine. It was designed to modularize the large code base of the Java platform.
A module system provides the same benefits for programming in the large. A module can make classes and packages selectively available so that its evolution can be controlled.
Local variable type inference (var)
Since Java 10.
// Pre-Java 10 |
HTTP client
Preview in Java 9. Standard Java 11.
With Java 11, Java got its own, modern client. It can be used to request HTTP resources over the network. It supports HTTP/1.1 and HTTP/2, both synchronous and asynchronous programming models, handles request and response bodies as reactive-streams
Example 1: A synchronous HTTP request
HttpClient client = HttpClient.newHttpClient(); |
Example 2: A asynchronous HTTP request
HttpClient client = HttpClient.newHttpClient(); |
JShell
JShell is an interactive tool introduced in Java 9 that allows for the execution of Java code snippets without the need for a full class structure.
Language and library enhancements
Java 9 enhancements
1. Collections
- List.of()
- Set.of()
- Map.of()
2. Streams
- takeWhile()
- dropWhile()
- iterate()
3. Optionals
- ifPresentOrElse()
4. Interfaces
Interfaces got private methods.
public interface MyInterface { |
Java 11 enhancements
Strings
- isBlank()
- lines()
- strip()
Files
- writeString()
- readString()
Java 17
Java 17 was released in 2021. In this section, cover Java 12~17 features.
Switch expressions
Preview in Java 12. Standard in Java 14.
Switch expressions can now return a value. And you can use a lambda-style syntax for your expressions, without the fall-through/break issues:
import static java.time.DayOfWeek.*; |
Text blocks / Multiline Strings
Preview in Java 13. Standard in Java 15.
String htmlBeforeJava13 = "<html>\n" + |
instanceof pattern matching
Preview in Java 14. Standard in Java 16.
The old way
if (obj instanceof String) { |
The new way
if (obj instanceof String s) { |
Records
Preview in Java 14.
The old way
final class Point { |
The new way
record Point(int x, int y) { |
Sealed classes
Preview in Java 15. Standard in Java 17.
Specify what classes can subclass current class.
public sealed class A permits B, C { |
For example,
public abstract sealed class Shape |
Java 21
Java 21 was released in 2023. In this section, cover Java 18~21 features.
Virtual threads (Project Loom)
Virtual threads are lightweight threads that reduce the effort of writing, maintaining, and debugging high-throughput concurrent applications.
Like a platform thread, a virtual thread is also an instance of java.lang.Thread. However, a virtual thread isn’t tied to a specific OS thread. A virtual thread still runs code on an OS thread. However, when code running in a virtual thread calls a blocking I/O operation, the Java runtime suspends the virtual thread until it can be resumed. The OS thread associated with the suspended virtual thread is now free to perform operations for other virtual threads.
Thread thread = Thread.ofVirtual() |
Pattern matching for Switch
Adds a more expressive and safer switch.
References
[1] Core Java (13th, 2024)
[2] Java Versions and Features
[3] Java 8 vs Java 11 vs Java 17 vs Java 21: A Comprehensive Comparison