Switch Statement

Switch Statement


switch statement gives the possibility to compare a variable with a list of values. 

A switch's expression must evaluate to a char, byte, short, int, an enum (from Java 6) and String (from Java 7). Any other type, like a float, will generate a compiler error. The switch can only check for equality. Such relational operators as greater than cannot be used.

Example 1. Syntax of switch-case-default Operator in Java

switch(expression) {
   case value1 : // Statements
   case value2 : // Statements
   default : // Statements
}

The case constants should evaluate to the same type as the switch expression. And it should be not only a final but a compile time constant as well. For example:

Example 2. Using of not Compile Time Constants in case Expression

final int a1 = 0;
final int a2;
a2 = 1;
int x = 2;
switch (x) {
    case a1: // ok
    case a2: // compiler error
}

If a switch expression evaluate to a value smaller than int (byte, short or char), case constants value should correspond to this type. For example this code won't compile:

Example 3. Using of byte Values in switch Expression

byte number = 2;
switch (number) {
    case 13:
    case 129://compiler error
}

It is illegal to have more than one case constant with the same value. For example, the following block of code won't compile:

Example 4. Multiple case Constants with the same Expression

int number = 90;
switch (number) {
    case 50:
        System.out.println("50");
    case 50:
        System.out.println("50"); //compile error
    case 140:
        System.out.println("140");
    default:
        System.out.println("default");
}

A switch expression can contain Character, Byte, Short and Integer values as well:

Example 5. Using of Wrapper Type in the switch Expression

switch (new Integer(9)) {
    case 9:
        System.out.println("9");
}

Case constants are evaluated from the top down, and the first case constant that matches the switch's expression is the execution entry point. The associated code block and ALL subsequent code blocks are executed. For example:

Example 6. The switch-case Expression without break

Vegetable p = Vegetable.potato;
switch (p) {
    case tomato:
        System.out.print("tomato ");
    case potato:
        System.out.print("potato ");
    case cucumber:
        System.out.print("cucumber ");
    default:
        System.out.println("any");
}

 The output will be:

potato cucumber any

To exit the switch statement the break keyword is used.

Example 7. The switch-case Expression with break

For example let's modify the previous example to have printed only one item:

Vegetable p = Vegetable.potato;
switch (p) {
    case tomato:
        System.out.print("tomato ");
        break;
    case potato:
        System.out.print("potato ");
        break;
    case cucumber:
        System.out.print("cucumber ");
        break;
    default:
        System.out.println("any");
}

The output will be:

potato 

The default section handles all values that are not explicitly handled by one of the case sections. The default case doesn’t have to come at the end of the switch:

Example 8. Using of the Default case

int z = 8;
switch (z) {
    case 1:
        System.out.println("Fall to one");
    default:
        System.out.println("default");
    case 3:
        System.out.println("Fall to three");
    case 4:
        System.out.println("Fall to four");
}

Starting from Java 7 it is possible to use String objects in the switch expression. Let's see how to use String in switch case in java:

Example 8. Using of the String Object in the switch Expression

String exam = "OCPJP 8";
switch (exam) {
     case "OCPJP 7":
         System.out.print(exam + ": 1Z0-804");
         break;
     case "OCPJP 8":
         System.out.print(exam + ": 1Z0-809");
         break;
     default:
         System.out.print(exam + ": --");
         break;
}

The String in the switch expression is compared with each case constant using the String.equals(...) method. So check for null before the switch block. For example, the following code will end with NullPointerException:

Example 9. Using of the String Object in the switch Expression which ends with NullPointerException

String exam = null;
switch (exam) {
    case "OCPJP 7":
        System.out.print(exam + ": 1Z0-804");
        break;
    case "OCPJP 8":
        System.out.print(exam + ": 1Z0-809");
        break;
    default:
        System.out.print(exam + ": ----");
        break;
}

 

Sources:  SCJP Sun Certified Programmer for Java 6 Exam 310-065



0 comments
Leave your comment: