What will be the result of compilation and execution of the following code?

public class Test {
    static void methodA(Long l) {
        System.out.println(l);
    }
    
    static void methodB(Object o) {
        Byte b = (Byte) o;
        System.out.println(b);
    }
    
    public static void main(String[] args) {
        byte b = 5;
        
        methodA(b);  // 1
        methodB(b);  // 2
    }
}
Explanation
The compiler does not apply widening + autoboxing when calling a method, but it can apply them in reversed order.
For methodB only boxing has to be applied because Byte is an Object.

Follow CodeGalaxy

Mobile Beta

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