I have this class, of a problem that I can't understand yet. I have some questions.
- Where java cast char to int (I know char is numeric type, but in the switch cases I have int and char values)
- Why case 1 not executed if switch postincrement set 1 value in the "i" variable. I think i value is 1 in the first iteration due the ++ operator.
- Why case 2 is executed first or How i get 2 value without get 1 before? I see the default of the first iteration print 1.
This is the code:
public class ForSwitch {
public static void main(String args[]) {
char i;
LOOP: for (i = 0; i < 5; i++) {
System.out.println("For: i value: " + (int) i);
switch (i++) {
case '0':System.out.println("A");
case 1:System.out.println("B");break LOOP;
case 2:System.out.println("C");break;
case 3:System.out.println("D");break;
case 4:System.out.println("E");
case 'E':System.out.println("F");
default:System.out.println("Switch: i value: " + (int) i);
}
}
}
}
Output is this:
For: i value: 0
Switch: i value: 1
For: i value: 2
C
For: i value: 4
E
F
Switch: i value: 5