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
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.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Зареєструйся Зараз
або Підпишись на майбутні тести