HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Flow Control / Switch Statement

Switch Statement in Java

As if-else statement tells your program to execute a certain section of code only if a particular test evaluates to true or false, the Java switch statement can have multiple possible execution paths.

A switch works with the –

  • primitive data types e.g. byte, short, char, and int
  • wrapper classes e.g. Character, Byte, Short, and Integer
  • enumerated types (added in java 5)
  • String class (added in java 7)

1. Java switch statement syntax

The general form of a switch statement is –

switch (switch-expression) 
{
        case label-1:
                statements;
 		break;

        case label-2:
                statements;
 		break;

        case label-3:
                statements;
 		break;

        default:
                statements;
}

Here label-1, label-2, etc. are “compile-time constant expressions” (the value of the labels must be known at compile time).

We can achieve the similar functionality with nested if-else blocks, but switch statement is more readable and clean.

2. Java switch statement execution flow

A switch statement is evaluated as follows:

  1. The switch-expression is evaluated.
  2. If the value of the switch-expression matches a case label, the execution starts from the matched case label and executes all statements until the break statement is encountered.
  3. If the value of the switch-expression does not match a case label, execution starts at the statement following the optional default label and continues until the end of the switch statement or the break statement is encountered.

You can easily make out that the use of a break statement inside the default label is not necessary because the default label is the last label in the switch statement and the execution of the switch statement will stop after that anyway.

3. Expression values must be in range

Please note that the value of the constant expressions used as the case labels must be in the range of the data type of switch-expression.

The range of the byte data type in Java is -128 to 127, so the following code would not compile because the second case label is 150, which is outside the range of the byte data type:

byte b = 10;
switch (b) {
        case 5:
                b++;
                break;

        case 150:       // A compile-time error. 150 is greater than 127
                b--;
                break;

        default:
                b = 0;
}

4. Distinct labels

Another important point to note is that two case labels in a switch statement cannot be the same. The following piece of code would not compile because case label 10 is repeated:

int num = 10;
switch (num) {
        case 10:
                num++;
                break;

        case 10: // A compile-time error. Duplicate case label 10
                num--;
                break;

        default:
                num = 100;
}

That’s all for switch statement in java.

Happy Learning !!

Read More : Java Control Statements

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Flow Control

  • Java Flow Control Statements
  • Java if-else Statement
  • Java Switch Statement
  • Java for Loop
  • Java for-each Loop
  • Java while Loop
  • Java do-while Loop
  • Java break Statement
  • Java continue Statement
  • Java Labeled Statement

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces