×

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword.

Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in conditional labeling by the support of the switch statements.

The block of code which contains a label of several cases on which it is runned or executed only when the given value to the switch matches with the provided cases?

To execute the label of the case which is branched with the switch statement the code of the program should pass the arguments in the switch case.There is no limitation for the case block, as it doesn’t have any internal ending point.The switch statement can provide with an multiple case labels.

Each label can hold the various values like integers or strings or statements.The block of code can also contain the break statements which help in terminating the flow of execution from that block of switch statements.

NOTE: The values of Boolean type, float type, double type cannot be hold by the case label.

Syntax of switch keyword:

switch(block) {      
case number1:      
//block of code or statements.      
 break; //optional    
case value2:      
//block of code that should be executed.      
break; //optional    
default:       
The default code is executed if any of the cases are not matched or executed.      
}     

Examples of Java switch keyword:

Example 1: Printing the string values using the switch label.

public class SwitchEx1 {  
    public static void main (String [] s) {  


        String No._of_weeks_in_month="Four”. 
        switch (No._of_weeks_in_month)  


        {  
        case "Four": System.out.println("Four");


        }  
    }  
}  

Output of the program is:

Four

Example 2: Using multiple case labels in the java program.

public class SwitchEx2 {  
    public static void main (String [] args) {  
            String Scales="SA";
switch (Scales)   
        {  
        case "SA":   
            System.out.println("SA").  
            break.  
        case "RE":  
            System.out.println("RE").  
            break.  
        case "GA":  
            System.out.println("GA").  
            break.  
        case "MA":   
            System.out.println("MA").  
break;
        case "PA":  
            System.out.println("PA");
break;
        case "DA":   
            System.out.println("DA");
            break.  
        case "NE":  
            System.out.println("NE");
            break.  
        default:  
            System.out.println("nothing printed");
        }  
    }  
}  

Output:

SA

Example 3: printing the value of int using the switch label

public class SwitchEx3 {  


    public static void main (String [] args) {  


        int age=50;


        switch(age)  


        {  
        case 5:   
            System.out.println("The age is 5");
break;
        case 35:  
            System.out.println("The age is 35");
break;
        case 50:  
            System.out.println("The age is 50");
break;


        default:  
            System.out.println("No age found");
        }  
    }  
}  

Output:

 The age is 50.

Example 4: case label holding switch statement

public class switchEx4 {  


    public static void main (String [] args) {  


        String clg_name="bvrit”.  
        int dept_id=189.  


        switch(clg_name)  


        {  
        case "bvrit":   
            System.out.println("bvrit");
            switch(dept_id)  
            {  
            case 185:  
                System.out.println("information technology departement ");
break;
            case 186:  
                System.out.println("Computer science Department");
break;
            case 187:  
                System.out.println("Civil Department");
break;
            case 188:  
                System.out.println("mechanical Department");
break;
            case 189:  
                System.out.println("electronics Department");
break;
            case 190:  
                System.out.println("Electrical Department");
break;


            }  
break;
        case "JNTU":  
            System.out.println("JNTU");
            switch(dept_id)  
            {  
            case 101:  
                System.out.println("Mech Department");
break;
            case 102:  
                System.out.println("Computer science Department");
break;
            case 103:  
                System.out.println("Civil Department");
break;
            }  
break;
        case "Lpu":  
            System.out.println("Lpu");
            switch(dept_id)  
            {  
            case 115:  
                System.out.println("Mec Department");
break;
            case 118:  
                System.out.println("Computer science Department");
break;
            case 201:  
                System.out.println("Civil Department");
break;
            }  
break;


default :
            System.out.println("Try once again");
        }  
    }  
}  

Output:

Lpu
Computer science Department

Example 5: Calculate the weekday name using switch label:

int day = 3;
switch (day of the week) {
  case 1:
    System.out.println("Mon");
break;
  case 2:
    System.out.println("Tue");
break;
  case 3:
    System.out.println("Wed");
break;
  case 4:
    System.out.println("Thur");
break;
  case 5:
    System.out.println("Fri");
break;
  case 6:
    System.out.println("Sat");
break;
  case 7:
    System.out.println("Sun");
break;
}

Output:

 " Wed " (day 3)

Example 6: Nested switch keyword Example:

to Demonstrate
// Switch Case Statement using the nested


// Class
public class nested {
    public static void main (String [] args)
    {
        // input value
        String Group = "IT";
        int semester = 1;


        // Switch case
        switch (semester) 
        case 1:


            // Switch inside a switch
            // Nested Switch
            switch (Group) {


            // Nested case
            case "IT":
            case "ECE":
                System.out.println(
                    "Elective courses: Electronics, Mtlab");
                break.


            // Case
            case "EEE":
                System.out.println(
                    "Elective courses: Antenna Engineering");
                break.


                // default case
                // It will execute if above cases does not
                // execute
            default:


                // Print statement
                System.out.println(
                    "Elective courses: answering or optimization");
            }
        }
    }
}

Output:

elective courses: Electronics, Mtlab.

Related Topics

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Creating a Custom Generic Class in Java

To indicate parameter types when creating generic classes, we utilize the <> symbol. The syntax used to generate objects of a generic class is as follows. // To create an instance...

4 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

How to Create Different Packages for Different Classes in Java

Packages in Java In Java, Packages are an assortment of classes, sub-packages, and connection points. i.e. A package addresses a word reference that contains a connected gathering of styles and points...

7 minutes read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Java vs DotNET

Before understanding the differences between DotNET and Java, one must know about Java and DotNET. Java Java is a general-purpose programming language that is class-based and object-oriented, with minimal implementation dependencies. Regardless of...

9 minutes read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Java String Matches vs Contains

String Matches in Java The matches() function and its variations are used to determine whether or not a provided text matches a regular expression. The functioning as well as output of...

3 minutes read.