Getting familiar with Java: Switch Case, Break and Default statement

So far in this series of Java tutorial under “Getting familiar with Java” we have brought  upon so many facets of Java from where we could find many instances of how code could run and in the mean time we also find some short cuts in order to make the code precise and response time of code should work intuitively and nice. In this tutorial we will love to study about switch case statement of Java and how it works. As usual let us create the class with Eclipse.

So after opening Eclipse then go to FILE and then to NEW and then to CLASS and it will open another window namely, NEW JAVA CLASS and as with previous article I have told you, in the name of the class write down ‘ExampleSwitchCase’ and then move to tick, PUBLI STATIC VOID MAIN inside WHICH METHOD  STUBS WOULD YOU LIKE TO CREATE. So, here we have created the class namely, ‘ExampleSwitchCase’ and the method namely PUBLIC STATIC VOID MAIN.

Then click on FINISH to complete the auto creation of ‘ExampleSwitchCase.java’. Why the name of the java file is exactly the same as that of the name of the class. This is the rule with Java the name of the java file should be same as that of the name of class file.  So now the auto generated java file is as follows.

package day1.examples;



public class ExampleSwitchCase {



       public static void main(String[] args) {

             



       }



}

 

This means, the entire java file is inside day1.examples folder of java installation. The package in java means folder. Then the class which is public and it is ExampleSwitchCase. The name of the class should be in bold letters of each of the word inside it so here the class name has three words such as Example and then Switch and then Class and all of these three words are in capital letters and there should not be any space between these words.

Now let us write the Switch Case Statement. It is as follows and keep a look at it and then I will explain it bit by bit.

package day1.examples;



public class ExampleSwitchCase {



       public static void main(String[] args) {

      

              int j = 2;

             

              switch(j) {

             

              case 0:

                     System.out.println("Value is 0");

                  break;

             

              case 1:

                     System.out.println("Value is 1");

                  break;

                 

                 

              case 2:

                     System.out.println("Value is 2");

                  break;

               

              case 3:

                     System.out.println("Value is 3");

                  break;

                 

              default:

                     System.out.println("No Value");

                  break;

              }

              }



       }

 

So, in the previous paragraph I explain in detail about package, class method which we create automatically through Eclipse. Now, first declare the integer variable as:

int j =2;

This is self explanatory as we have seen, that it is a statement so presence of semicolon at the end of it. Then the next line of code is:

switch(j) {

switch statement of integer j and beginning of  opening curlybrace. Then begins various cases of this switch J. Here, it is.

case 0:

System.out.println(“Value is 0”);

break;

 

case 1:

System.out.println(“Value is 1”);

break;

 

 

case 2:

System.out.println(“Value is 2”);

break;

 

case 3:

System.out.println(“Value is 3”);

break;

 

We do provide three case of switch statement. In the case 0, it asks the compiler if the value of integer j is 0 then print it as value is 0 and if that value is 0 then it will not shift to the following other cases. That is why the presence of break statement is there and it is a statement and that is why the presence of semicolon. I hope still this phase you have now cleared idea of case 0 and then move to case 1 and if the value of j is 1 then compiler should print it other wise move to case 2 and here if the value of integer j is 2 then compiler please print this as value is 2.

So, we have declared the value of integer j as 2 and this means that, it is true and then comes the break statement so it will not go  to check out the case 3. In the end we have default statement which is as follows after writing one switch and three cases.

default:

System.out.println(“No Value”);

break;

 

Here, the default statement begins and if no value of case 0, 1,2 and 3 satisfies the integer then switch to default which says no value. Then put the closing curly brace.

Now, save this java file and then right click anywhere at the code window and then run as and then run as Java application and the compiler will run and will show the result as follows.

Value is 2

After reaching to case 2 it finds that value is 2 so it prints the value is 2 and then comes the break statement so it will not go to check again case 3 and then default statements. Now we will see when we stop the break statement or commented it and see what will be the compiler output. So just put comment mark of the break statement after case 2. The result will be

Value is 2

Value is 3

It comes because, it checks case 0 and then 1 and then finds out that both of these statements are not correct and then it reaches to case 2 and here it is correct and then it prints and there is no break statement after case 2 so it automatically print out the case 3.That is why presence of both case 2 and case 3 is there.

Now the question rises what is the role of default statement after switch cases. Let us now look at it. Suppose our code is like as below.

package day1.examples;

 

public class ExampleSwitchCase {

 

public static void main(String[] args) {

 

int j = 5;

 

switch(j) {

 

case 0:

System.out.println(“Value is 0”);

break;

 

case 1:

System.out.println(“Value is 1”);

break;

 

 

case 2:

System.out.println(“Value is 2”);

break;

 

case 3:

System.out.println(“Value is 3”);

break;

 

default:

System.out.println(“No Value”);

break;

}

}

 

}

 

 

Here, is the new code with slight modification here. Here, we do find that, we changed the declaration of value of integer from 2 to now 5 and this means in each of the four switch cases we do not have the values satisfies the integer as it is upto 3 and this means that, it will print out the last default statement which is of no values as all the switch and cases does not satisfy the value of integer and so ours compiler output will be as follows.

No Value

Leave a Reply

Your email address will not be published. Required fields are marked *

You are here

  • Home
  • Java
  • Getting familiar with Java: Switch Case, Break and Default statement