Switch Case Program in Java

What is a switch statement in Java?
The switch case statement in Java is a control flow statement that allows a program to execute different blocks of code based on the value of an expression. It replaces multiple if-else statements and improves the readability of the code. The switch statement evaluates the expression and executes the corresponding code block for the matching case.

Syntax of Switch Statement:

switch (expression)
    case value1:
        // code to be executed
    case value2:
        // code to be executed
        // default code to be executed

Key Considerations for using Switch Statements:

  • The expression used in a switch statement must evaluate to a primitive type (such as int, byte, short, or char) or an enumerated type.
  • Each case statement must contain a constant expression that matches the data type of the switch expression.
  • The switch statement must contain at least one case statement, and may also contain a default statement.
  • The break statement is used to exit a switch statement, and is required after each code block that is executed. If a break statement is not included in a code block, all subsequent code blocks will be executed, regardless of whether they match the expression or not.
  • The default statement is optional, and is executed if none of the case statements match the expression.
  • The order of the case statements does not matter, as the matching is based on the value of the expression and not the order of the cases.
Problem Description

Create a Java program that demonstrates how to use strings in switch statements.

Problem Solution

Enter the string as input. Now we match the given string with different cases and then generate the output accordingly.

Program/Source Code

Here is the source code of the Java Program to Use Strings in Switch Statements. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Strings_Switch
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         String week;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter choice:");
  9.         week = s.nextLine();
  10.         switch (week) 
  11.         {
  12.             case "Monday":
  13.             System.out.print("Day: Monday");
  14.             break;    
  16.             case "Tuesday":
  17.             System.out.print("Day: Tuesday");
  18.             break;
  20.             case "Wednesday":
  21.             System.out.print("Day: Wednesday");
  22.             break;
  24.             case "Thursday":
  25.             System.out.print("Day: Thursday");
  26.             break;
  28.             case "Friday":
  29.             System.out.print("Day: Friday");
  30.             break;
  32.             case "Saturday":
  33.             System.out.print("Day: Saturday");    
  34.             break;
  36.             case "Sunday":
  37.             System.out.print("Day: Sunday");   
  38.             break;    
  39.         }
  40.     }
  41. }
Program Explanation

1. The code begins by importing the java.util.Scanner class, which is used to read input from the user.
2. A new class called Strings_Switch is defined, which contains the main method.
3. Inside the main method, a variable called week of type String is declared to store the user’s input for the day of the week.
4. A new Scanner object named s is created to read input from the user, using System.in to represent the standard input stream.
5. The user is prompted to enter their choice using the System.out.print method, and the input is read using the nextLine method of the Scanner object and stored in the week variable.
6. The switch statement is used to check the value of the week variable and execute the corresponding code block depending on the case. If none of the cases match, the code block following the default case would execute (which is not present in this code).
7. Each case in the switch statement checks if the week variable matches a particular day of the week string literal, e.g., “Monday”, “Tuesday”, etc.
8. If a match is found, the corresponding System.out.print statement is executed to display the day of the week.
9. Each case ends with a break statement to exit the switch statement and continue with the rest of the code.

Note: Join free Sanfoundry classes at Telegram or Youtube

Time Complexity: O(1)
The time complexity of a switch statement in Java is O(1) or constant time because it jumps directly to the matching case without evaluating any other case, and the time required does not depend on the size or number of cases.

Space Complexity: O(1)
The space complexity of a switch statement in Java is O(1) or constant space because it does not allocate any additional memory other than the required variables and cases.

Program Output:

The program output will be as follows when “Saturday” is provided as the input:

$ javac Strings_Switch.java
$ java Strings_Switch
Enter choice:Saturday
Day: Saturday

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.