Java SE 11 - Java 语言规范中类型转换的新案例

标签 java type-conversion java-11 autoboxing jls

JLS §5.2 Java SE 11 包含一些新的类型转换案例,Java 8 的 JLS 没有,请参阅列表中的第 4 项和第 5 项:

Assignment contexts allow the use of one of the following:

  1. an identity conversion
  2. a widening primitive conversion
  3. a widening reference conversion
  4. a widening reference conversion followed by an unboxing conversion
  5. a widening reference conversion followed by an unboxing conversion, then followed by a widening primitive conversion
  6. a boxing conversion
  7. a boxing conversion followed by a widening reference conversion
  8. an unboxing conversion
  9. an unboxing conversion followed by a widening primitive conversion

我不明白 案例 4 案例5 在列表中。谁能用例子给我一些解释?如果可能,还请说明其实际用途。

更新:
作为@Naman commented ,这里是更改 JLS 的建议 - JDK-8166326 : 5.2: Allow widening before unboxing从 Java-9 开始生效。在报告中,它提到:

This behavior is especially important for interoperability with capture: various existing programs expect to be able to treat the elements of a List<? extends Integer> as if they were ints.

List<? extends Integer> li = null;
int i = li.get(0);

这可能暗示着这次 JLS 的改变确实有实际的必要性。但我还是不明白为什么 很重要。什么与捕获的互操作性 意思是什么,为什么重要?这些有什么用各种现有程序看起来像?它们是 Java 代码吗(我知道其他一些语言也适用于 JVM,并且可能与 Java 代码有交互)?

最佳答案

TL;博士

…The problem with boxing is that it is [ad-hoc] and expensive; extensive work has gone on under the hood to address both of these concerns“ — Brian Goetz, State of Valhalla, March 2020


这让我怀疑您所引用的规范的那些更改正在为上述链接中讨论的 L-World 的计划中的“代码像类,像 int 一样工作”功能准备语言。

…Could anyone give me some explanation with examples?…


评论中链接到的 2016 年错误报告中的示例( plus the 2013 one that one links to )给出了 的最佳示例#4 在你的 list 中。基于这些,这里有一个 的例子#5 在你的 list 中……
< I extends Integer > void soAns0( I intRef ) {

   int intPrim;

   long longPrim = /* (3) a widening primitive conversion */
   intPrim =       /* (2) an unboxing conversion */
   intRef;         /* (1) a widening reference */
}

…If possible, please also explain its practical usage…


natural numbers通常被称为“整数”。 Java Language Tutorial显示了将自然数限制为类型 Integer 的一种实际用法。 …
public class NaturalNumber<T extends Integer> {

    private T n;

    public NaturalNumber(T n)  { this.n = n; }

    public boolean isEven() {
        return n.intValue() % 2 == 0;
    }

    // …
}
RowFilter<M,​I> 的 API 文档中也有这个例子…
…
  public boolean include(Entry<? extends PersonModel, ? extends Integer> entry) {
    …
    Person person = personModel.getPerson(entry.getIdentifier());
    …
  }
…
另一个用例是 T extends Integer如果您想明确禁止您的类使用任何其他类型的 Number 进行参数化,这可能是合适的。 明确禁止 添加 Integers到那个参数化类型——通过利用 Get/Put Principle .
假设您有一些代码,给定一些数字,将创建该数量的对象。
如果您想通过使用 Long.MAX_VALUE 轰炸您的系统来禁止恶意行为者压倒您的系统DOS 尝试次数,边界 <T extends Short>可能是限制在任何一个方法调用中创建的对象数量的一种方法。

…What do these various existing programs look like? Are they java code?…


在 github 上搜索 T extends Integer出现 eighty-thousand some hits .

…What does interoperability with capture mean?…


我将捕获转换的最终解释推迟到 the JLS itself .

…and why is it important?…


捕获转换很重要,因为它与类型推断有关。
书写方式‹Integer <: α›the JLS' section on type inference或多或少是一种正式的表达方式 Integer extends Integer (T extends Integer 实际上意味着什么)...

From Arrays.asList(1, 2.0), we have the constraint formulas ‹1 → α› and ‹2.0 → > α›. Through reduction, these will become the constraint formulas ‹int → α› and ‹double → α›, and then ‹Integer <: α› and ‹Double <: α›.


鉴于此,JDK uses <T extends Integer> a lot for Generics/Type Inference related tests 也就不足为奇了。 JDK 本身。
这是my absolute favorite use of <T extends Integer> in the wild .
不过,大多数情况下,我敢打赌,规范中那些与拆箱/扩展相关的更改与 Valhalla 有很大关系。

关于Java SE 11 - Java 语言规范中类型转换的新案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63662871/

相关文章:

iOS:将 id 转换为 int

在事件 JDK 中找不到 JavaFX 部署库

java - 带有 JDK 11 的 TOMCAT 7.0.92 :- ERROR localhost-startStop-1 org. apache.catalina.startup.ContextConfig - 无法处理 Jar tools.jar 的注释

java - 如何通过 ftp 入站 channel 适配器轮询本地 ftp 目录?

java - 类型不匹配将 char[] 转换为对象

java - 未在 firebase 中创建字符串的数组列表

python - Pandas read_sql 数据类型

java - Mac OS上的OpenJDK8和JavaFX

java - 避免通过 jsonschema2pojo 生成 setter 方法

java - 启动简单的非基于 Web 的 Java 应用程序的官方 Spring Boot 方式是什么?