The expression in a switch statement should be a compile time constant ie it can take int , byte , char, short ie any type which can be cast into an int . If instead of int i take a byte argument as in the following code :-
byte g=1;
switch(g){
case 23:
case 128:
}
This results in compile time error as the case argument 128 is too large for a byte . My question is g has already been implicitly converted to an int . Now so it is an integer which is being compared in the case statement . Then why are we getting the error .
This message was edited 1 time. Last update was at by ragi singh
My question is g has already been implicitly converted to an int
No it will not be implicitly converted to an int. The case constants must be assignable to the switch expression. Since the switch expression is of type byte, the value 128 is out of its range so you get an error...