I have a question about the code below. The code is taken from my Programming Languages book.
byte x, y, z;
...
/* The values of y and z are coerced into int and int addition is performed */
/* The sum is converted into byte */
x = y + z;
My question is why Java does a coercion like that. Do you have any ideas?
Thanks in advance.
In the JVM, every stack element has a size of 32 bits. The actual addition works like this:
int
)iadd
instruction is called, which pops two values from the stack and adds themThis is why you have to cast the resulting value (of type int
) to a byte
again.