The switch statement has the following form:
1 2 3 4 5 6 7 8 |
switch (expression) { [case constant1: statement_or_block [break;]] [case constant2: statement_or_block [break;]] … [default: statement-or-block [break;]] } |
[ ] = optional Example:
1 2 3 4 5 6 7 8 |
byte b = 'a'; switch (b) { case 97: System.out.println("a"); break; case 98: System.out.println("b"); break; default: System.out.println("c"); } |
The 6 things you must remember about the switch statement are: It works with the following data types: byte/Byte, short/Short, int/Integer, char/Character, String and enum All case labels should be compile time constants (literals or final variables) ..Continue Reading