What will be the result of the following code execution?

public class Main {
    public static void main(String[] args) {
        byte b = 12;
        byte k = b + 1;
        System.out.println(k);
    }
}
Explanation
When performing arithmetic operations with values ​​of type byte, char, short they are implicitly converted into the type int. Therefore, the result of the expression b + 1 will also be of type int. For it to be able to be assigned to a variable k, you need to use explicit type conversion:

byte k = (byte)(b + 1);

Следи за CodeGalaxy

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

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