Java Wrapper in Switch Statements

Java provides four wrapper classes to use: Integer, Short, Byte, and Long in switch statements.

Example:

Java Wrapper in switch case.

Java
public class WrapperSwitchExample {

    public static void main(String[] args) {
        Integer age = 25;

        switch (age.intValue()) { // Extract primitive value for switch
            case 25:
                System.out.println("You are 25.");
                break;
            case 30:
                System.out.println("You are 30.");
                break;
            default:
                System.out.println("Age not matched.");
        }
    }
}

Output:

You are 25.

Note:

Regardless of its placement, the default case only gets executed if none of the other case conditions are met. So, putting it at the beginning, middle, or end doesn’t change the core logic (unless you’re using a less common technique called fall-through).

Example: In this code we will identify the weekday through (1-7) numbers.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a day number (1-7): ");
        int day = scanner.nextInt();

        switch (day) {
            default:
                System.out.println("Not a valid weekday.");
                break;
            case 1:
                System.out.println("It's Monday!");
                break;
            case 2:
                System.out.println("It's Tuesday!");
                break;
            case 3:
                System.out.println("It's Wednesday!");
                break;
            case 4:
                System.out.println("It's Thursday!");
                break;
            case 5:
                System.out.println("It's Friday!");
                break;
            case 6:
                System.out.println("It's Saturday!");
                break;
            case 7:
                System.out.println("It's Sunday!");
                break;
        }
    }
}

Output

Enter a day number (1-7): 8
Not a valid weekday.


Switch Statements in Java

The switch statement in Java is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions.

It is like an if-else-if ladder statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. The expression can be a byte, short, char, or int primitive data type. It tests the equality of variables against multiple values.

Note: Java switch expression must be of byte, short, int, long(with its Wrapper type), enums and string. Beginning with JDK7, it also works with enumerated types (Enums in java), the String class, and Wrapper classes.

Similar Reads

Syntax

switch(expression){ case value1 : // Statements break; // break is optional case value2 : // Statements break; // break is optional .... .... .... default : // default Statement}...

Flowchart of Switch-Case Statement

This flowchart shows the control flow and working of switch statements:...

break in switch case Statements

A break statement is optional. If we omit the break, execution will continue into the next case....

Java Nested Switch Statements

We can use a switch as part of the statement sequence of an outer switch. This is called a nested switch. Since a switch statement defines its block, no conflicts arise between the case constants in the inner switch and those in the outer switch....

Java Enum in Switch Statement

Enumerations (enums) are a powerful and clear way to represent a fixed set of named constants in Java....

default statement in Java Switch Case

default case in the Switch case specifies what code to run if no case matches....

Case label variations

Case label and switch arguments can be a constant expression. The switch argument can be a variable expression....

Java Wrapper in Switch Statements

Java provides four wrapper classes to use: Integer, Short, Byte, and Long in switch statements....

Exercise

To practice Java switch statements you can visit the page: Java Switch Case statement Practice...

Conclusion

Switch statements in Java are control flow structures, that allow you to execute certain block of code based on the value of a single expression. They can be considered as an alternative to if-else-if statements in programming....

Java Switch Statements- FAQs

How to use switch statements in Java...