Java 23 新特性全览:值类型、模式匹配与字符串模板正式版

Complete Guide to Java 23: Value Types, Pattern Matching, and String Templates GA

| iDev Tech | 2026-07-11T17:11:34

Java 23 于2026年9月正式发布,包含值类型(Valhalla 项目)首个预览、模式匹配全面完成、字符串模板正式版等重磅特性。本文梳理所有关键 JEP 并提供实践示例。

Java 23 officially releases in September 2026 with first preview of value types (Project Valhalla), complete pattern matching, string templates GA, and more. This article covers all key JEPs with practical examples.

Java 23:Valhalla 终于来了随着 Java 23 在2026年9月的正式发布,Java 开发者期待已久的多项重大特性终于落地。其中最令人兴奋的无疑是 Project Valhalla 的值类型首个预览版。值类型(Preview)值类型是 Java 内存模型的根本性革新。通过 value class 关键字声明的类型在堆上以扁平布局存储,没有对象头开销,不支持身份比较(==比较值而非引用)。在数值密集型应用中,值类型数组的内存占用可减少60-80%,缓存友好度大幅提升。模式匹配最终完成原始类型模式匹配:switch 表达式现在支持对 int、double 等原始类型直接匹配数组模式匹配:可以解构数组的前N个元素并绑定到变量嵌套记录模式增强:支持任意深度的记录嵌套解构Guard 表达式语法优化:when 子句更加简洁直观字符串模板正式版经过三个预览版的打磨,字符串模板功能正式转为稳定特性。STR 和 FMT 模板处理器开箱即用,RAW 模板处理器支持自定义 SQL、JSON 等领域特定格式化。编译期类型检查确保模板中的表达式类型安全。其他值得关注的 JEPScoped Values 正式版替代 ThreadLocal;流收集器(Stream Gatherers)正式版提供自定义中间操作能力;隐式声明的类和实例主方法正式化,简化入门级 Java 程序编写。


Java 23: Valhalla Has Finally ArrivedWith Java 23's official release in September 2026, several long-awaited major features finally land. The most exciting is undoubtedly the first preview of value types from Project Valhalla.Value Types (Preview)Value types represent a fundamental innovation in Java's memory model. Types declared with the value class keyword are stored in flat layout on the heap, with no object header overhead and no identity comparisons (== compares values, not references). In numerically intensive applications, memory usage of value type arrays can be reduced by 60-80% with dramatically improved cache friendliness.Pattern Matching CompletionPrimitive type pattern matching: switch expressions now support direct matching on int, double, and other primitive typesArray pattern matching: destructure the first N elements of an array and bind them to variablesEnhanced nested record patterns: support arbitrary depth record nested destructuringGuard expression syntax improvements: when clauses are more concise and intuitiveString Templates GAAfter three preview iterations, string templates are now a stable feature. STR and FMT template processors work out of the box, while the RAW template processor supports custom domain-specific formatting for SQL, JSON, and more. Compile-time type checking ensures type safety of expressions within templates.Other Notable JEPsScoped Values GA replaces ThreadLocal; Stream Gatherers GA provides custom intermediate operation capabilities; implicitly declared classes and instance main methods are finalized, simplifying introductory Java programs.

← Back to News