What will be the result of this code compiling and executing:

public class ByteTest {
    public static void main(String[] s) {
        byte b = 8;
         m(b);
         m(7);
    }

    static void m(byte b) {
        System.out.print("byte");
    }
}
Explanation
If the assignment command use a constant expression with a value of type int, falling within the range of byte, the compiler automatically narrows its type. That is why the byte b = 8; command compiles without errors.
In other cases, for example during the method call, this will not occur. Therefore, to the command m(7); compiler error is generated.

Follow CodeGalaxy

Mobile Beta

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