Java - What is the output: switch case value range

Question

What is the output:

public class Main {
  public static void main(String[] args) {
    byte b = 10;
    switch (b) {
    case 5:
      b++;
    case 150: 
      b--;
    default:
      b = 0;
    }
    System.out.println(b);
  }
}


Click to view the answer

case 150: // A compile-time error. 150 is greater than 127

Note

The value of the constant expressions used in the case labels must be in the range of the data type of switch-expression.

The range of the byte data type in Java is -128 to 127. case label 150 is outside the range of the byte data type.

Related Quiz