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有效,并且现已解决。
最佳答案
您找到的文本出现在 JLS section 5.6 中。值得注意的是:
- 这是介绍性描述性文本,而不是规范性文本。
- 它没有准确说明“算术”运算符在此上下文中的含义。
- 相反,它并没有说数字提升不适用于(可以说)不是“算术”运算符的其他运算符。
如果您继续阅读 5.6.1 部分和 5.6.2 ,您将找到一元和二进制数字提升适用的运算符。
请注意,上述内容适用于 JLS 版本 5 和 11。到 JLS 14,他们已将第 5.6.1 和 5.6.2 节折叠到第 5.6 节中。 。措辞已更改(删除您认为矛盾的文字)。相关运营商全部(仍然)列出。
(这是编辑整理,而不是实际语言语义的变化。)
关于java - 数字提升仅适用于算术运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63415067/