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);

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes