Search code examples
javatype-conversionjlstype-promotion

Numeric promotion only for arithmetic operators?


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.


Solution

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