Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.10. The switch statement

3.10. The switch statement
Last updated: 27 January 2013.

The switch statement allows you to execute statements based on a primitive type value, an enum value or a string value (you can use string values since Java 1.7). In fact, the switch statement is an alternative to the if-else-if statement. Let's take a look at the following example:

public class SwitchStatement {

   
public void test(int x){

         
if(x == 0){
               
System.out.println("x equals 0");
         
}
         
else if(x == 1){
               
System.out.println("x equals 1");
         
}
         
else if( x == 2 ){
               
System.out.println("x equals 2");
         
}
         
else{
               
System.out.println("x is not equal to 0 or 1 or 2");
         
}
    }

}

You can replace the if-else-if statement with a switch statement as shown in the code below:

public class SwitchStatement {

   
public void test(int x){

         
switch(x) {
         
case 0 : {
               
System.out.println("x equals 0");
               
break;
         
}
         
case 1 : {
               
System.out.println("x equals 1");
               
break;
         
}
         
case 2 : {
               
System.out.println("x equals 2");
               
break;
         
}
         
default : {
               
System.out.println("x is not equal to 0 or 1 or 2");
               
break;
         
}
          }
    }

}

The default block will be executed if the value of x is not equal to any of the case values. The keyword break exits the switch statement. Let's call the method test in the class App:

public class App {

   
public static void main(String[] args) {

         
new SwitchStatement().test(1);

   
}

}

The output is:

x equals 1

If you don't add the break statements at the end of the case blocks, the matching case block is executed but all the following case blocks are also executed. See the code below:

public class SwitchStatement {

   
public void test(int x){

         
switch(x) {
         
case 0 : {
               
System.out.println("x equals 0");
         
}
         
case 1 : {
               
System.out.println("x equals 1");
         
}
         
case 2 : {
               
System.out.println("x equals 2");
         
}
         
default : {
               
System.out.println("x is not equal to 0 or 1 or 2");
         
}
          }
    }

}

The output is:

x equals 1
x equals 2
x is not equal to 0 or 1 or 2

You can have several case clauses separated by colons in a single statement:

public class SwitchStatement {

   
public void test(int x){

         
switch(x) {
         
case 0 : case 1 : {
               
System.out.println("x equals 0 or 1");
               
break;
         
}
         
case 2 : {
               
System.out.println("x equals 2");
               
break;
         
}
         
default : {
               
System.out.println("x is not equal to 0 or 1 or 2");
               
break;
         
}
          }
    }

}

The output is:

x equals 0 or 1

An enum type can be used in a switch statement. In the next example, the method printOrdinal prints the ordinal of the passed enum field to the standard output:

public final class SwitchStatement {

   
enum Color { RED, GREEN, BLUE };

   
public void printOrdinal(Color color){

         
switch(color) {
         
case RED : {
               
System.out.println(color.ordinal());
               
break;
         
}
         
case GREEN : {
               
System.out.println(color.ordinal());
               
break;
         
}
         
case BLUE : {
               
System.out.println(color.ordinal());
               
break;
         
}
         
default : {
               
System.out.println("Unknown color");
               
break;
         
}
          }
    }


   
public static void main(String[] args) {

         
new SwitchStatement().printOrdinal(Color.GREEN);

   
}

}

The output is:

1

Although the main method should be in a separate class, this time, I wrote it within the class SwitchStatement to avoid declaring the enum type as a static member (static members will be discussed in a later tutorial).


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.10. The switch statement
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.11. The for statement

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7