java - 为什么在Java中切换Integer包装器时, 'char'情况无法编译,但切换到Byte时编译正常?

标签 java java-8 switch-statement

无法编译:

void test(Integer x) {
      switch (x) {
          case 'a':
      }
}
<小时/>

编译成功:

void test(Byte x) {
      switch(x) {
          case 'a':
      }
}

最佳答案

原因相当复杂,但它们都包含在 Java 语言规范的详细信息中(细则,如果您愿意的话)。

首先,JLS 14.11关于 switch 语句如下:

"Every case constant associated with the switch statement must be assignment compatible with the type of the switch statement's Expression (§5.2)."

这意味着 'a' 需要分别分配给 IntegerByte

但这听起来不对:

  • 您可能会认为,由于 'a' 应该可以分配给Integer,因为 char -> int 赋值是合法的。 (任何 char 值都适合 int。)

  • 您可能会认为,因为 'a' 不应该分配给字节,因为char -> byte 赋值不合法。 (大多数 char 值无法装入字节。)

事实上,这两种说法都不正确。要了解原因,我们需要阅读 JLS 5.2实际上是关于赋值上下文中允许的内容。

"Assignment contexts allow the use of one of the following:

  • an identity conversion (§5.1.1)
  • a widening primitive conversion (§5.1.2)
  • a widening reference conversion (§5.1.5)
  • a widening reference conversion followed by an unboxing conversion
  • a widening reference conversion followed by an unboxing conversion, then followed by a widening primitive conversion
  • a boxing conversion (§5.1.7)
  • a boxing conversion followed by a widening reference conversion
  • an unboxing conversion (§5.1.8)
  • an unboxing conversion followed by a widening primitive conversion."

要从 'a' 变为 Integer,我们需要1char 值扩大到int 然后将 int 装箱为 Integer。但是,如果您查看允许的转换组合,您将无法先进行扩大原始转换,然后再进行装箱转换。

因此,不允许将 'a' 转换为 Integer。这解释了第一种情况下的编译错误。

您可能会认为 'a'Byte 是不允许的,因为这将涉及原始缩小转换......这根本不在列表中。事实上,文字是一种特殊情况。 JLS 5.2继续说以下内容。

"In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:

  • A narrowing primitive conversion may be used if the variable is of type byte, short, or char, and the value of the constant expression is representable in the type of the variable.

  • A narrowing primitive conversion followed by a boxing conversion may be used if the variable is of type Byte, Short, or Character, and the value of the constant expression is representable in the type byte, short, or char respectively."

其中第二个适用于 'a'Byte,因为:

  • 字 rune 字是常量表达式,并且
  • 'a' 的值为 97 十进制,在 byte 的范围内 (-128+127)。

这解释了为什么第二个示例中没有编译错误。

<小时/>

1 - 我们无法将 'a' 装箱为 Character,然后将 Character 扩大为 Integer 因为 Character 不是 Integer 的 Java 子类型。如果源类型是目标类型的子类型,则只能使用扩大引用转换。

关于java - 为什么在Java中切换Integer包装器时, 'char'情况无法编译,但切换到Byte时编译正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58243530/

相关文章:

java - 如何在Java中使用cURL?

javascript - es6 中 case 后的 switch 语句中的大括号有什么作用?

php - 在不同的文件中拆分开关案例

列中带有 ImageView 数组的 JavaFX TableView

java - 向 TextField 添加提示

java - LWJGL 3、VAO 和 Shader 不渲染

java - 如何组合Switch语句?

java - Java 8中 "functional interface"的精确定义

java - 返回 Optional<User> 而不是里面的 Optional<BigDecimal>

crash - Java 8 应用程序使用所有系统 RAM,然后因 SIGBUS 崩溃。这里发生了什么?