数字提升仅适用于算术运算符?

Numeric promotion only for arithmetic operators?

JLS 声明数字提升应用于算术运算符的操作数。

Numeric promotion is applied to the operands of an arithmetic operator. Numeric promotion contexts allow the use of: an identity conversion (§5.1.1) a widening primitive conversion (§5.1.2) an unboxing conversion (§5.1.8)

但是,根据我的经验,我发现数字提升也适用于其他运算符(如按位运算符)的操作数。我发现 this 指出

These conversions can occur when using the multiplicative operators (%, *, /), additive operators (+, -), comparison operators (<, >, <=, >=), equality operators (==, !=), and the integer bitwise operators (&, |, ^).

我是不是漏掉了什么?

编辑:未列出的其他运算符,如 &&、||、>>、<<、>>> 等,如何处理?

编辑 2:正如@Turing85 和@Stephen C 所指出的,此问题仅对 JLS 5 至 11 有效,现已解决。

Binary numeric promotion (JLS 5.6.2) 适用于“某些二元运算符”的操作数,其中包括按位运算符 &、^ 和 |。引用:

Binary numeric promotion is performed on the operands of certain operators:

  • The multiplicative operators *, /, and % (§15.17)
  • The addition and subtraction operators for numeric types + and - (§15.18.2)
  • The numerical comparison operators <, <=, >, and >= (§15.20.1)
  • The numerical equality operators == and != (§15.21.1)
  • The integer bitwise operators &, ^, and | (§15.22.1)
  • In certain cases, the conditional operator ? : (§15.25)

对于&&和||,这些是布尔运算符,并且有数字提升。

移位运算符>><<>>>遵循不同的规则:unary numeric promotion分别应用于操作数,并确定表达式的类型仅由左侧操作数。这意味着以下代码有效:

int i =1;
long l = 2;
int j = i << l;

您找到的文本出现在 JLS section 5.6 中。值得注意的是:

  1. 这是介绍性描述性文本,不是规范性文本。
  2. 它没有准确说明“算术”运算符在这种情况下的含义。
  3. 相反,它并不是说数字提升不适用于其他(可以说)不是“算术”运算符的运算符。

如果您继续阅读 5.6.1 and 5.6.2 部分,您会发现一元和二进制数值提升适用于的运算符。

请注意,上述内容适用于 JLS 第 5 版和第 11 版。到 JLS 14,他们已将第 5.6.1 和 5.6.2 节折叠到 5.6 节中。措辞已更改(删除您认为矛盾的文本)。相关运算符全部(仍)列出。

(这是社论 tidy-up,并非实际语言语义的改变。)