Site Search:

Use a switch statement

Back OCAJP

switch(kid)
switch(kid)

Switch is one of the flow control statement, its syntax is:
switch(variable) {
  case constantExpression1:
    statements1;
    break;
  case constantExpression2:
    statements2;
  ...
  default:
    statementsN;
    break;
  ...
}

From top to bottom, if variable matches the first constantExpression value, the corresponding statements are then executed; after that, the flow goes to cases downstairs without evaluating constantExpression any more. Any time it meets a "break;" statement, the flow then goes out of switch, skipping all the code below. The default is a special case, which matches everything else the other constantExpressions didn't cover.

The allowed variable types includes: byte, Byte, short, Short, int, Integer, char, Character, String, enum values. (Note there is no boolean!) The constantExpressions have to match variable type, and they have to be final constant.

That's all we have to know about switch statement, now let's practice.


OCAJP>cat test.java 
class test{
  public static void main(String...args) {
    switch(args[0]) {
      case "dog": System.out.print(" dog ");break;
      case "cat": System.out.print(" cat ");
      default: System.out.print(" pet ");
      case "mice": System.out.print(" mice "); break;
      case "fish": System.out.print(" fish ");
      case "lizard": System.out.print(" lizard ");
      case "tiger":break;
      case "lion":
      case "bug": System.out.print(" bug ");
    }
    System.out.println("pet lover");
  }
}
OCAJP>javac test.java 
OCAJP>java test
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at test.main(test.java:3)
OCAJP>java test dog
 dog pet lover
OCAJP>java test cat
 cat  pet  mice pet lover
OCAJP>java test ooooo
 pet  mice pet lover
OCAJP>java test mice
 mice pet lover
OCAJP>java test tiger
pet lover
OCAJP>java test lion
 bug pet lover
OCAJP>java test fish
 fish  lizard pet lover
OCAJP>



switch mixed with numbers


OCAJP>cat test.java 
class test{
  public static void main(String...args) {
    //short s = (short)2 + 1, d = Integer.MAX_VALUE; //possible lossy conversion from int to short
    short s = (short)(2 + 1); 
    final short a = 1, b = 5, c = 3, d = (short)Integer.MAX_VALUE;
    switch(s) {
      case d: s += 10;
      case 5: break;
      default: System.out.println("default will be the first entry if nothing matches");
      case c: s += s++-++s; System.out.println("s="+s);
      case 11: s = 3; s = (short)(s + s++-++s); System.out.println("case 11:"+s);
      case 92: s = 3; s = (short)(s + (s++)-(++s)); System.out.println("case 12:"+s);
      case 61: s = 3; s = (short)((s + (s++))-(++s)); System.out.println("case 12:"+s);
      //case 100L: //not compile   
      case 1: System.out.println("exit point");
      //case a: //duplicate label
    }
    int i = 3;
    System.out.println(-++i);
    //(i=3);  //not a statement
    i = 3;
    System.out.println(+--i);
  }
}
OCAJP>javac test.java 
OCAJP>java test
s=1
case 11:1
case 12:1
case 12:1
exit point
-4
2


know what's not work


OCAJP>cat test.java 
class test{
  public static void main(String...args) {
    final char b;
    final char a = (b = 'A');
    char grade = 'B';
    final char _match$ = 'C';
    System.out.println("a="+a+",b="+b);
    int x = 10;
    int y = x++-++x;  
    y = x--+--x;
    switch(grade) {
      case 'A': System.out.println(b!=a^5L>=3); //boolean a = true; //boolean a already defined
      case _match$: 
      case 'B': long m = 25; byte i = 20; i+=++i; m = true&true?true^true?50:i:20; System.out.println("m="+m); //41
      default: int j = (byte)1.0f; grade = 'A'; 
      case 'D': System.out.println("exit point"); break;
      case 'F': System.out.println("y = " + y); break;  //long m = 2; //long m already defined
      //case y:  //possible lossy conversion from int to char
      //case (char)m: //constant expression required
      //case 'C': //duplicate case label
      //case grade:  //error: constant expression required
      //case a:  //error: constant expression required
      //case b:  //error: constant expression required
      //case B:  //error: Cannot find symbol
    }
    System.out.println("out y="+y);  //22
    ;;;;;
    //++y += ++x;  //unexpected type
    //y = x+++++x; //unexpected type
    //y = x-----y; //unexpected type
  }
}

OCAJP>javac test.java
OCAJP>java test
a=A,b=A
m=41
exit point
out y=22

Back OCAJP