error: –enable-preview must be used with either -source or –release
JDK 21版本正在积极开发中,预计将于9月19日左右发布,目前尚未达到通用可用(GA)状态。该版本提供了多项预览功能。
我下载了其中一个发布版本进行尝试。实际上几周前我已经尝试过,所以已经安装了该版本。
例如:
|
|
有趣的是,有几个非常酷的功能仍处于预览阶段。例如,我尝试使用InfoQ博客中提到的StringTemplates:
|
|
要启用这些功能进行编译,请使用以下示例命令:
|
|
最后,运行程序时也需要启用预览功能:
|
|
Java 21中包含的JEP列表:
JEP | 标题 |
---|---|
430 | String Templates (Preview) |
431 | Sequenced Collections |
439 | Generational ZGC |
440 | Record Patterns |
441 | Pattern Matching for switch |
442 | Foreign Function & Memory API (Third Preview) |
443 | Unnamed Patterns and Variables (Preview) |
444 | Virtual Threads |
445 | Unnamed Classes and Instance Main Methods (Preview) |
446 | Scoped Values (Preview) |
448 | Vector API (Sixth Incubator) |
449 | Deprecate the Windows 32-bit x86 Port for Removal |
451 | Prepare to Disallow the Dynamic Loading of Agents |
452 | Key Encapsulation Mechanism API |
453 | Structured Concurrency (Preview) |