error: –enable-preview must be used with either -source or –release
JDK 21版本正在稳步推进,预计将在9月19日左右发布,目前还不是正式版。更重要的是,它提供了预览功能。
是时候下载其中一个版本并进行尝试了。实际上我在几周前已经尝试过,所以已经安装了它。
例如:
|
|
有趣的是,有几个非常酷的功能仍然处于预览状态。例如,我尝试使用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) |