DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java BasicsTopic 31 of 59
←PreviousPrevNextNext→

Java Switch Statement

Introduction to Switch

The switch statement in Java allows selecting one block of code to execute out of multiple possible branches, based on the value of a single variable or expression.

It is often used as a more readable and efficient alternative to long chains of if-else statements when checking the same variable against different constant values.

Example
public class SwitchIntro {
    public static void main(String[] args) {
        int day = 3;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
            default:
                System.out.println("Invalid day");
        }
    }
}
Output
Wednesday

How It Works

The expression inside the switch is evaluated once, and its result is compared with the values of each case label.

When a match is found, the code for that case runs until a break statement is reached or the switch block ends.

If no case matches, the default block executes (if provided).

Switch with Strings

Since Java 7, switch statements can also operate on String values.

This is useful for handling multiple constant string comparisons, such as user input or command keywords.

Example
public class SwitchWithStrings {
    public static void main(String[] args) {
        String fruit = "Apple";

        switch (fruit) {
            case "Apple":
                System.out.println("It's an apple.");
                break;
            case "Banana":
                System.out.println("It's a banana.");
                break;
            default:
                System.out.println("Unknown fruit.");
        }
    }
}
Output
It's an apple.

Switch Without Break

If a break statement is missing, execution continues into the next case (a behavior called fall-through).

This can be intentional when multiple cases should share logic, but can cause errors if done accidentally.

Example
public class SwitchFallThrough {
    public static void main(String[] args) {
        int number = 2;

        switch (number) {
            case 1:
                System.out.println("One");
            case 2:
                System.out.println("Two");
            case 3:
                System.out.println("Three");
            default:
                System.out.println("Default case");
        }
    }
}
Output
Two
Three
Default case

Enhanced Switch (Java 14+)

Java 14 introduced the enhanced switch expression, which allows using switch as an expression that returns a value.

It uses the arrow (->) syntax to eliminate fall-through and can directly assign results to variables.

Example
public class EnhancedSwitch {
    public static void main(String[] args) {
        int day = 5;
        String dayName = switch (day) {
            case 1 -> "Monday";
            case 2 -> "Tuesday";
            case 3 -> "Wednesday";
            case 4 -> "Thursday";
            case 5 -> "Friday";
            case 6 -> "Saturday";
            case 7 -> "Sunday";
            default -> "Invalid day";
        };

        System.out.println(dayName);
    }
}
Output
Friday
Test your knowledge: Java Switch Statement
Quiz Configuration
4 of 10 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java BasicsTopic 31 of 59
←PreviousPrevNextNext→