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
Зареєструйся Зараз
або Підпишись на майбутні тести