What will be the result of the program?

class A {
    static byte m1() {
      final char c1 = '\u0001';
      return c1;                                  // 1
    }
    static byte m2(final char c2) { return c2; }  // 2
    public static void main(String[] args) {
        char c3 = '\u0003'; 
        System.out.print("" + m1() + m2(c3));         // 3
    }
}
Explanation
Compilation error in line 2 arises from the inability to perform an implicit conversion of the type in return (char) to the type specified in the method header (byte).
Errors can be avoided if you perform an explicit type conversion: "return (byte) c2;"
In a similar situation in line 1 compilation error does not occur because c1 - is a constant and in fact the compiler works with the expression "return 1" and this value falls within the range byte.

Follow CodeGalaxy

Mobile Beta

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