Latest in branch Java SE 18
18.0.2.1
Released 18 Aug 2022
(3 years ago)
SoftwareJava/Java SE
BranchJava SE 18
Status
End of life
Initial release18
22 Mar 2022
(4 years ago)
Latest release18.0.2.1
18 Aug 2022
(3 years ago)
Java class file format major version62.0
End of
premier support
Sep 2022
(Ended 3 years, 7 months ago)
Release noteshttps://www.oracle.com/java/technologies/javase/18-0-2-1-relnotes.html
Documentationhttps://docs.oracle.com/javase/18
Downloadhttps://www.oracle.com/java/technologies/javase/jdk18-archive-downloads.html
Java/Java SE Java SE 18 ReleasesView full list

What Is New in Java 18

Category Feature JEP
New Feature Simple Web Server JEP 408
New Feature Code Snippets in Java API Documentation JEP 413
New Feature Internet-Address Resolution SPI JEP 418
Preview Feature Pattern Matching for switch JEP 420
Incubator Feature Vector API (Third Incubator) JEP 417
Core Lib UTF-8 by Default JEP 400
Core Lib Reimplement Core Reflection with Method Handles JEP 416
Deprecation Finalization Deprecated for Removal JEP 421

What new tools and APIs does Java 18 introduce?

Java 18 adds a minimal HTTP server for prototyping and testing. The jwebserver command starts a static server, eliminating the need for external tools during development. This matters because it streamlines quick setup for serving files locally.

The Javadoc @snippet tag simplifies including example code in API documentation. It validates syntax and integrates better with IDEs than previous comment-based approaches. In practice, this makes documentation more reliable and easier to maintain.

A new SPI for hostname resolution allows pluggable strategies beyond the OS default. This is crucial for modern applications that need custom DNS or service discovery mechanisms without complex workarounds.

How does Java 18 improve pattern matching?

Pattern matching for switch is now in its second preview (JEP 420). It enhances switch expressions and statements with pattern matching capabilities, allowing more expressive and concise code. This reduces boilerplate when dealing with complex conditional logic based on types and values.

You can now use guarded patterns directly in switch labels and enjoy improved null case handling. This evolution brings us closer to a more declarative style of Java programming, similar to functional languages.

static String formatterPatternSwitch(Object obj) {
    return switch (obj) {
        case Integer i -> String.format("int %d", i);
        case Long l    -> String.format("long %d", l);
        case Double d  -> String.format("double %f", d);
        case String s  -> String.format("String %s", s);
        default        -> obj.toString();
    };
}

What core library enhancements are included?

UTF-8 is now the default charset across all implementations and platforms (JEP 400). This resolves inconsistencies where APIs would silently use the platform default charset, leading to unpredictable behavior. Your applications will now behave consistently across different environments unless explicitly overridden.

Core reflection is reimplemented using method handles (JEP 416). This reduces the maintenance cost of the java.lang.reflect.Method and Constructor classes. For most developers, this is an internal change with no visible impact, but it modernizes the platform's foundation.

What is the status of the Vector API?

The Vector API enters its third incubator phase (JEP 417). It expresses vector computations that compile to optimal hardware instructions on supported CPU architectures. This API is for advanced developers working on high-performance computing tasks like scientific simulations or machine learning.

Performance has been improved based on feedback from previous incubations. While still not a final API, it's becoming more stable and expressive for writing platform-agnostic complex vector algorithms.

What is being deprecated in Java 18?

Object finalization has been deprecated for removal (JEP 421). The finalization mechanism is inherently flawed and has been a source of performance and reliability issues for years. You should have already migrated to try-with-resources and cleaner APIs for resource management.

This deprecation finally signals the endgame for this problematic feature. Modern Java applications should use java.lang.ref.Cleaner or PhantomReference for cleanup operations instead.

FAQ

Is the new jwebserver tool production-ready?
No, the Simple Web Server is designed for testing, development, and educational purposes only. It's not intended for production use due to its minimal feature set and lack of security hardening.

Will UTF-8 as default charset break existing applications?
It might affect applications that relied on the platform default charset without explicitly specifying encoding. You should test applications that perform string-to-byte conversions without specifying charset to ensure compatibility.

When should I use the Vector API instead of traditional loops?
Consider the Vector API when you need to perform identical operations on large arrays of data where performance is critical. For general-purpose programming, traditional loops remain simpler and more than adequate.

What should I replace object finalization with?
Use try-with-resources for closeable resources and java.lang.ref.Cleaner for other cleanup tasks. The Cleaner API provides more predictable cleanup without the drawbacks of finalization.

Can I use pattern matching in switch expressions now?
Yes, but you must enable preview features since it's still in preview status. This means using --enable-preview compiler flags, and the syntax might still change in future Java versions based on feedback.

Releases In Branch Java SE 18

VersionRelease date
18.0.2.118 Aug 2022
(3 years ago)
18.0.219 Jul 2022
(3 years ago)
18.0.1.102 May 2022
(4 years ago)
18.0.119 Apr 2022
(4 years ago)
1822 Mar 2022
(4 years ago)