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 meantime we also find some shortcuts 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:

  1. 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.
  2. In the name of the class write down ‘ExampleSwitchCase’ and then move to tick, PUBLIC STATIC VOID MAIN inside WHICH METHOD STUBS WOULD YOU LIKE TO CREATE.
  3. So, here we have created the class namely, ‘ExampleSwitchCase’ and the method namely PUBLIC STATIC VOID MAIN.
  4. Then click on FINISH to complete the auto-creation of ‘ExampleSwitchCase.java’.
  5. Why the name of the java file is exactly the same as that of the name of the class.
  6. This is the rule with Java the name of the java file should be same as that of the name of the class file.
  7. 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.
  • The class name has three words such as Example and then Switch.
  • 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 keeps 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 the presence of semicolon at the end of it.
  • Then the next line of code is:

switch(j) {

  • switch statement of integer j and the beginning of opening curly brace.
  • 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 the compiler should print it otherwise 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 a 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 a 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 the 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 the slight modification here.
  • Here, we do find that we changed the declaration of the value of integer from 2 to now 5.
  • This means in each of the four switch cases we do not have the values satisfies the integer as it is up to 3.
  • This means that it will print out the last default statement which is of no values as all the switch and cases do not satisfy the value of the integer.
  • So our compiler output will be as follows.

No Value


Related Posts:

  1. Getting familiar with Java: And && Or II statements
  2. Java vs Kotlin: Which is the better option for Android App development
  3. Learning interface of Android Studio Integrated Development Environment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.