JLS 10, 14.11. The switch Statement wrote:Every case constant associated with the switch statement must be assignment compatible with the type of the switch statement's Expression
Since in your example Switch statement's expression is of type byte whose range is -128 to 127 and in switch statement's case you have 127 + 1 results 128 which doesn't fit in the range of byte (-128 To 127 ) so gives error.
If it was 126 + 1 results in 127 which is by default int literal still would run successfully because 127 is assignment compatible with byte means It fits in the range of byte data type.
Ganesh Patekar wrote:. . . 127 is implicitly narrowed to byte...
But that sort of thing can only occur without an explicit cast when you are declaring and initialising a variable on the same line, and the expression right of the = is a compile‑time constant.
I don't think so It can happen in Switch statement's case constant and expression assignment as well. I tried to compile the following codeIt gave compile time error:
Test.java:6: error: incompatible types: possible lossy conversion from int to byte byte no = 127 + 1;
Test.java:11: error: incompatible types: possible lossy conversion from int to byte case 128:
In both cases It tried implicitly narrowing compile time constant value 128 ( int literal ) to byte so gave same error message for both i.e. possible lossy conversion from int to byte means at both places It tried to convert those values into byte but couldn't.