0

If I compile this:

 1      public class test {
 2          public static void main (String args[]) {
 3              byte bx = 1;
 4              byte by = 2;
 5
 6              int iz = bx ^ by;
 7              byte bz = bx ^ by;
 8              byte cbz = (byte)(bx ^ by);
 9          }
10      }

then I get this error:

test.java:7: error: incompatible types: possible lossy conversion from int to byte
                bz = bx ^ by;

This led me on a brief journey resulting in me deciding ^ always results in an int in a bitwise context. I don't understand why that choice was made.

At first I thought it might have something to do with boxing (or unboxing) but I'm using a primitive type and so I don't think that is part of my confusion.

The only thing I can think of is that byte is promoted to int but I haven't found anything yet that says that's what happens.

3
  • 1
    This says "binary numeric promotion is first performed on the operands". Following that link to here says this: "Otherwise, both operands are converted to type int." Commented Apr 27, 2018 at 19:01
  • Those aren't really duplicates because they don't say "why" the promotion occurs. I'm guessing it's because they don't have to write 16 different versions of the same code to prevent developers from having to do the seemingly unnecessary cast. Commented Apr 27, 2018 at 19:10
  • 1
    @jeff6times7 The answer in second link does try to explain the deeper why, rather than just my answer below, which basically says: Because that's the way they wrote it in the specification. Commented Apr 27, 2018 at 19:10

1 Answer 1

3

Because, in Java, all numeric operators will promote operands to int, long, float, or double, as defined by JLS 5.6.2. Binary Numeric Promotion:

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:

  1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).

  2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

    • If either operand is of type double, the other is converted to double.

    • Otherwise, if either operand is of type float, the other is converted to float.

    • Otherwise, if either operand is of type long, the other is converted to long.

    • Otherwise, both operands are converted to type int.

After the type conversion, if any, value set conversion (§5.1.13) is applied to each operand.

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.