Java Switch Statement

Video Tutorial
FREE
Switch case thumbnail
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

Switch statement in Java is used to select one of the many code blocks to be executed. The value of the expression in switch case in Java is compared with the values of each case.

If there is a match, the associated block of code is executed. Switch case in Java is fall-through which means it executes all the cases after the first match if a break statement is not encountered.

Switch Statement in Java

Some Important Points to Remember

Let’s quickly go through some standard rules for writing switch cases in Java.

  • No variables: The case value in switch statement must be a literal or constant. Also, it must be of the same type as switch expression.
  • No duplicates: No two cases in a switch statement should be of same value. Otherwise, compilation error is thrown.
  • Allowed Types: The Java switch expression must be of int, long, byte, short, enums, and String type. Primitives are allowed with their wrapper types.
  • Optional Break Statement: Break statement is optional in Java's switch statement. If a case is matched and there is no break statement mentioned, subsequent cases are executed until a break statement or end of the switch statement is encountered.
  • Optional default case: default case value is optional. The default statement is meant to execute when there is no match between the values of the variable and the cases. It can be placed anywhere in the switch block according to the required result.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax

Let us understand the usage of switch case in Java programmatically.

Example

Consider the code snippet given below:

Output:

Explanation:

  • The value of the day variable is compared with each of the case values. Since day = 4, it matches the fourth case value, and Day 4: Thursday is printed.
  • The break statement in the fourth case breaks out of the switch statement.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

The break Keyword

When the Java program reads the break statement it comes out of the switch block immediately with no further comparisons. The execution of any more code within the switch-case block is seized and case testing/comparison is stopped.

The default Keyword

In switch case in Java, the default statement is executed when there is no single comparison that checks out which means the value of variable and the case value don’t match in of any cases. In this scenario, suppose the day variable is given the value 9 then all the cases will be checked and since equality conditions cannot be achieved with this value, the default statement will get executed at the end.

Example:

Output:

Java Nested Switch Statement

A nested switch case in a java statement refers to a scenario where a switch statement is used inside another switch statement. Basically, it is switch statement within a switch statement.

Example:

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • The Switch case in Java is the comparison between a variable and its possible values.
  • Break statement is used to come out of the switch block immediately without any further comparisons.
  • Default statement is executed only when there is no case with value same as the variable value.
  • Switch case is called a fall-through statement when we miss out break statements in switch cases.
  • We can nest one switch statement within another switch statement with independent variable values.
  • We can use enum and wrapper classes in switch statements in Java.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more