只有算术运算符才进行数字提升吗?

huangapple go评论114阅读模式
英文:

Numeric promotion only for arithmetic operators?

问题

《Java 语言规范》(JLS)指出,数值提升适用于算术运算符的操作数。

> 数值提升适用于算术运算符的操作数。
数值提升上下文允许使用以下转换:

  • 身份转换(§5.1.1)
  • 扩展原始类型转换(§5.1.2)
  • 拆箱转换(§5.1.8)

然而,根据我的经验,我发现数值提升也适用于其他运算符的操作数,比如位运算符。我在这里发现了这样的说明:

> 当使用乘法运算符(%,*,/)、加法运算符(+,-)、比较运算符(<,>,<=,>=)、相等运算符(==,!=)以及整数位运算符(&,|,^)时,这些转换可能会发生。

那么,我是不是遗漏了什么?

编辑:那么其他未列出的运算符如 &&、||、>>、<<、>>> 等呢?

编辑 2:正如 @Turing85 和 @Stephen C 指出的,在 JLS 5 到 11 版本中,这个问题已经得到解决。

英文:

The JLS states that numeric promotion is applied to the operands of an arithmetic operator.

> 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)

However, in my experience I found out that numeric promotion is also applied to the operands of other operators like bitwise operators. I found out this which states that

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

So am I missing something?

Edit: What about the other operators not listed like &&, ||, >>, <<, >>>, etc.?

Edit 2: As pointed out by @Turing85 and @Stephen C, this question is only valid for JLS 5 to 11 and has been resolved now.

答案1

得分: 1

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

> 在某些运算符的操作数上执行二进制数值提升:
>
> - 乘法运算符 *、/ 和 %(§15.17)
> - 数值类型的加法和减法运算符 + 和 -(§15.18.2)
> - 数值比较运算符 <、<=、> 和 >=(§15.20.1)
> - 数值相等性运算符 == 和 !=(§15.21.1)
> - 整数按位运算符 &、^ 和 |(§15.22.1)
> - 在某些情况下,条件运算符 ? :(§15.25)

至于 && 和 ||,这些是布尔运算符,存在数值提升。

位移运算符 &gt;&gt;&lt;&lt;&gt;&gt;&gt; 遵循不同的规则:对操作数分别应用一元数值提升,表达式的类型仅由左操作数决定。这意味着以下代码是有效的:

int i = 1;
long l = 2;
int j = i &lt;&lt; l;
英文:

Binary numeric promotion (JLS 5.6.2) applies to operands of "certain binary operators" which includes the bitwise operators &, ^, and |. Quote:

> 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)

As for && and ||, these are operators on booleans and there is numeric promotion.

Bit shift operators &gt;&gt;, &lt;&lt;, &gt;&gt;&gt; follow a different rule: unary numeric promotion is applied to the operands separately, and the type of the expression is determined solely by the left hand side operand. This means the following code is valid:

int i =1;
long l = 2;
int j = i &lt;&lt; l;

答案2

得分: 1

你找到的文本出现在JLS 第5.6节中。值得注意的是:

  1. 这是介绍性的描述性文本,而不是规范性文本。
  2. 它并没有准确说明在这个上下文中何为“算术”运算符。
  3. 反之,它并没有说明数值提升不适用于其他(有争议的)不是“算术”运算符的运算符。

如果你继续阅读5.6.1节5.6.2节,你会找到一元和二元数值提升适用于哪些运算符。

需要注意的是,上述内容适用于JLS第5版和第11版。到了JLS第14版,它们已将第5.6.1节和第5.6.2节合并到了第5.6节中。措辞有所改变(移除了你认为有矛盾的文本)。相关的运算符仍然被列出。

(这只是编辑上的整理,而不是实际语言语义的更改。)

英文:

The text you found appears in JLS section 5.6. It is worth noting the following:

  1. This is introductory descriptive text, not normative text.
  2. It does not say exactly what an "arithmetic" operator means in this context.
  3. Conversely, it does not say that numeric promotion does not apply to other operators that are (arguably) not "arithmetic" operators.

If you read on to sections 5.6.1 and 5.6.2, you will find the operators that unary and binary numeric promotions apply to.

Note that the above is true for JLS editions 5 and 11. By JLS 14 they have folded sections 5.6.1 and 5.6.2 into section 5.6. The wording has changed (removing the text that you thought was contradictory). The relevant operators are all (still) listed.

(This is an editorial tidy-up, not a change in the actual language semantics.)

huangapple
  • 本文由 发表于 2020年8月14日 22:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63415067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定