ExamVeda
Login
Home
Data Types and Variables
?

What is the output for the below code?
public class Test{
      public static void main(String[] args){
            byte b = 6;
            b+=8;
            System.out.println(b);
            b = b+7;
            System.out.println(b);
      }
}

Answer & Solution
Correct Answer: Option C

int or smaller expressions always resulting in an int. So compiler complain about Type mismatch: cannot convert from int to byte for b = b+7; But b += 7; // No problem because +=, -=, *=, and /= will all put in an implicit cast. b += 7 is same as b = (byte)b+7 so compiler not complain.

Examveda
Question posted by Examveda
Community

Join the Discussion

No comments yet Be the first to discuss this question.