shanghaishanghai

##Java 即将引入可选空值标记,提升代码安全性

**2024年8月22日,** 一份新的JEP草案(JEP 8303099)发布,旨在将可选空值标记引入到Java语言中,这将为Java开发者带来更安全的代码编写体验。

该草案借鉴了Kotlin的标记方式,为类型引入三种使用方式:

* **Foo!**表示Null-Restricted,这种类型可接受的值不包括null。
* **Foo?** 表示Nullable,这种类型可接受的值包括null。
* **Foo** 没有明确指定是否接受null,作为默认选项,保持现有代码的编译时语义不变。

该提案要求对每一个类型的使用进行注解,目前尚不支持将整个类或模块标记为Null-Restricted。

引入空值标记不仅增强了编译时的安全性,还支持逐步采用的策略。开发者可以先定义类型允许的空值,然后通过解决编译时警告来逐步优化代码。

**该草案与JSpecify项目存在重叠之处。** JSpecify项目旨在通过注解来指示静态类型的空值状态,为开发者提供更精确的类型信息。该草案的发布意味着JSpecify项目将能够平滑迁移到语言级别的空值标记,开发者可以轻松地将现有代码迁移到新的语法。

**该草案也与Project Valhalla项目存在关联。** Project Valhalla旨在提升Java的性能和效率,而空值限制是其中一个重要话题。知道哪些值不能为空将有助于虚拟机优化这些间接调用。

**该草案的发布意味着Java语言正在不断发展,以满足开发者对代码安全性和效率的更高要求。** 未来,随着该草案的进一步完善,Java开发者将能够编写更加安全、高效的代码,提升软件开发的整体质量。

英语如下:

##Java to Introduce Optional Null Type Annotations, Could NullPointerException Be a Thing ofthe Past?

**Keywords:** Java, null, annotation

## Java toIntroduce Optional Null Type Annotations, Enhancing Code Security

**August 22, 2024,** a new JEP draft (JEP8303099) has been released, aiming to introduce optional null type annotations into the Java language, offering Java developers a more secure coding experience.

The draft draws inspiration from Kotlin’s annotation approach, introducing three usage modes for types:

* **Foo!** represents Null-Restricted, meaning this type accepts values that do not include null.
* **Foo?**represents Nullable, meaning this type accepts values that include null.
* **Foo** does not explicitly specify whether it accepts null, serving as the default option and maintaining the existing compile-time semantics of current code.

The proposal requires annotationsfor every type usage, and currently does not support marking entire classes or modules as Null-Restricted.

Introducing null type annotations not only enhances compile-time security but also supports a gradual adoption strategy. Developers can first define the null-permissiveness of types and then gradually optimize their code by resolving compile-time warnings.

**The draft overlaps with the JSpecify project.** JSpecify aims to provide developers with more precise type information by using annotations to indicate the null state of static types. The release of this draft means that the JSpecify project will be able to smoothly migrate to language-level null type annotations, enabling developers to easilymigrate existing code to the new syntax.

**The draft also has connections with the Project Valhalla project.** Project Valhalla aims to improve Java’s performance and efficiency, with null restriction being a key topic. Knowing which values cannot be null will help the virtual machine optimize these indirect calls.

**The release of this draftsignifies the ongoing evolution of the Java language to meet developers’ increasing demands for code security and efficiency.** In the future, as the draft is further refined, Java developers will be able to write safer, more efficient code, enhancing the overall quality of software development.

【来源】https://mp.weixin.qq.com/s/gQmzTTex52fjJaS-eW2O-A

Views: 1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注