What will be the result of the following code execution?

public class Test {
    public static void main(String args[]) {
        byte a = 1;
        byte b = ++a;
        byte c = -a;

Compilation error will occur in the following line

byte c = -a;

since the result of the "-a" expression is of type int, despite the fact that the operand is byte. This is necessary to prevent possible loss of accuracy. For example, when a = -128 the received value exceeds a byte range.

It doesn't happen during an increment / decrement operations execution because, for example, for a = 127 the value of the ++a expression will be equal to -128.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Зарегистрируйся сейчас
или Подпишись на будущие тесты