Assigning a value to a local variable and then returning that at the end is considered a good practice.

I have no idea when it was considered a good practice. To me, switch is usually * an indicator that a design error was made. I would rather put my effort into thinking how to avoid a switch than into wondering how to return a value from a switch.

A few examples

Long list of if statements in Java
How to avoid switch-case statements in Java
Converting many 'if else' statements to a cleaner approach

Methods having multiple exits are harder to debug and can be difficult to read.

The same goes for a method that has a lot of breaks - that's what you are going to do if you choose the "local-variable approach".

In my opinion, none of these

// 1
switch (input) {
    case "A":
        return "1";
    case "B":
        return "2";
    default:
        return "0";
}

// 2
String varibleToReturn = null;
switch (input) {
    case "A":
        varibleToReturn = "1";
        break;
    case "B":
        varibleToReturn = "2";
        break;
    default:
        varibleToReturn = "0";
}
return varibleToReturn;

// 3
return switch(digitInDecimal) {
    case  0 -> '0';
    case  1 -> '1';
    case  2 -> '2';
    default -> '?';
}

makes a significant difference, or a slight improvement. Yes, Java-12's switch would give more conciseness and expressiveness, but the fundamental idea remains the same.

Must I wait for Java 12 where switch can be used without temporary variables and breaks?

What does it mean? :) No, the deadline is tomorrow, you have to work with what you've got at hand now.


*I am not underestimating the usefulness of switch. It may come in handy, for instance, when you programme at low-level, or you write an optimization.

I am just saying that in the real world, with Springs, and Hibernates, in a world of patterns, switch is obsolescent.

Answer from Andrew on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_switch.asp
Java Switch
The switch expression is evaluated once. The result is compared with each case value. If there is a match, the matching block of code runs. The break statement stops the switch after the matching case has run.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › switch.html
The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
The following code example, SwitchDemo, declares an int named month whose value represents a month. The code displays the name of the month, based on the value of month, using the switch statement.
People also ask

How does a Switch Statement differ from an If-else statement?
A Switch Statement and an If-else statement both control program flow based on conditions, but they differ in structure. Switch is ideal for multiple fixed values, offering a cleaner syntax. If-else is more flexible for complex conditions. Switch is efficient for specific value matching, while If-else provides broader conditional control.
🌐
theknowledgeacademy.com
theknowledgeacademy.com › blog › switch-case-java
Java Switch Statement with Syntax and Example
What happens if a break statement is omitted in a Switch Case?
If a break statement is omitted in a Switch Case, the program will continue to execute subsequent case statements, resulting in "fall-through." This can lead to unintended behavior, where multiple case blocks are executed. It's crucial to include break statements to ensure the desired Switch Case is executed and prevent the fall-through effect.
🌐
theknowledgeacademy.com
theknowledgeacademy.com › blog › switch-case-java
Java Switch Statement with Syntax and Example
What is Knowledge Pass, and how does it work?
The Knowledge Academy’s Knowledge Pass, a prepaid voucher, adds another layer of flexibility, allowing course bookings over a 12-month period. Join us on a journey where education knows no bounds.
🌐
theknowledgeacademy.com
theknowledgeacademy.com › blog › switch-case-java
Java Switch Statement with Syntax and Example
🌐
GeeksforGeeks
geeksforgeeks.org › java › switch-statement-in-java
Switch Statements in Java - GeeksforGeeks
The switch statement in Java is a multi-way decision statement that executes different blocks of code based on the value of an expression.
Published   1 month ago
🌐
Baeldung
baeldung.com › home › java › core java › java switch statement
Java Switch Statement | Baeldung
November 5, 2025 - The switch statement allows us to replace several nested if-else constructs and thus improve the readability of our code. Switch has evolved over time. New supported types have been added, particularly in Java 5 and 7.
🌐
Tutorialspoint
tutorialspoint.com › java › switch_statement_in_java.htm
Java - switch statement
Java switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
🌐
DataCamp
datacamp.com › doc › java › java-switch-statement
Java Switch Statement
The switch statement evaluates an expression, matches the expression's value against a series of case labels, and executes the block of code associated with the matching case.
Top answer
1 of 2
3

Assigning a value to a local variable and then returning that at the end is considered a good practice.

I have no idea when it was considered a good practice. To me, switch is usually * an indicator that a design error was made. I would rather put my effort into thinking how to avoid a switch than into wondering how to return a value from a switch.

A few examples

Long list of if statements in Java
How to avoid switch-case statements in Java
Converting many 'if else' statements to a cleaner approach

Methods having multiple exits are harder to debug and can be difficult to read.

The same goes for a method that has a lot of breaks - that's what you are going to do if you choose the "local-variable approach".

In my opinion, none of these

// 1
switch (input) {
    case "A":
        return "1";
    case "B":
        return "2";
    default:
        return "0";
}

// 2
String varibleToReturn = null;
switch (input) {
    case "A":
        varibleToReturn = "1";
        break;
    case "B":
        varibleToReturn = "2";
        break;
    default:
        varibleToReturn = "0";
}
return varibleToReturn;

// 3
return switch(digitInDecimal) {
    case  0 -> '0';
    case  1 -> '1';
    case  2 -> '2';
    default -> '?';
}

makes a significant difference, or a slight improvement. Yes, Java-12's switch would give more conciseness and expressiveness, but the fundamental idea remains the same.

Must I wait for Java 12 where switch can be used without temporary variables and breaks?

What does it mean? :) No, the deadline is tomorrow, you have to work with what you've got at hand now.


*I am not underestimating the usefulness of switch. It may come in handy, for instance, when you programme at low-level, or you write an optimization.

I am just saying that in the real world, with Springs, and Hibernates, in a world of patterns, switch is obsolescent.

2 of 2
2

But I found an old but high-ranked answer that says to avoid multiple return statements:

Assigning a value to a local variable and then returning that at the end is considered a good practice. Methods having multiple exits are harder to debug and can be difficult to read.

So I wonder, is that answer still relevant due to switch changes?

This is a common misconception, it originates form the phrase: "Single entry, single exit." (Page 24) All this originates from an other era, one that lead to structured programming languages and eventually to object oriented programming languages (like Java).

Don't worry about multiple return statements, there is nothing wrong with it.

Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › java flow control › switch statement
Switch Statement in Java
November 14, 2022 - 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; } Another important point to note is that two case labels in a switch statement cannot be the same.
🌐
HWS Math
math.hws.edu › javanotes › c3 › s6.html
Javanotes 9, Section 3.6 -- The switch Statement
A switch statement allows you to test the value of an expression and, depending on that value, to jump directly to some location within the switch statement. Only expressions of certain types can be used. The value of the expression can be one of the primitive integer types int, short, or byte.
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › switch-case-java
Java Switch Statement with Syntax and Example
The Java Switch statement is a branch statement that provides a way to execute your code in different cases based on the value of the expression. Read more!
🌐
Programiz
programiz.com › java-programming › switch-statement
Java switch Statement (With Examples)
In the above example, we have created a switch-case statement. Here, the value of expression doesn't match with any of the cases. Hence, the code inside the default case is executed. ... Wrapper Classes: Character, Byte, Short, and Integer. ... Before we wrap up, let’s put your knowledge of Java switch Statement (With Examples) to the test!
🌐
Oracle
docs.oracle.com › en › java › javase › 25 › language › switch-expressions-and-statements.html
11 Switch Expressions and Statements - Java
September 15, 2025 - Previous Next JavaScript must be enabled to correctly display this content · You can use the switch keyword as either a statement or an expression. Like all expressions, switch expressions evaluate to a single value and can be used in statements. Switch expressions may contain "case L ->" ...
🌐
Medium
medium.com › @AlexanderObregon › beginners-quick-guide-to-java-switch-statements-768c0757b98a
Beginner’s Quick Guide to Java Switch Statements
March 11, 2024 - Without it, all statements after the matched case until the end of the switch block (or the next break) would execute. The default case: It acts as a catch-all for any value not explicitly handled by the case statements. This is useful for error handling or providing a default operation. In some situations, multiple cases may require the same code to be executed. The Java switch statement allows these cases to be grouped together, reducing redundancy and enhancing clarity.
🌐
DataCamp
datacamp.com › doc › java › switch
switch Keyword in Java: Usage & Examples
The switch keyword in Java is used to execute one block of code among many alternatives. It is a control statement that allows the variable to be tested for equality against a list of values, each with its own block of code.
🌐
Medium
medium.com › javarevisited › understanding-the-java-switch-statement-a-comprehensive-guide-186d1d638d83
Understanding the Java Switch Statement: A Comprehensive Guide | by Mouad Oumous | Javarevisited | Medium
September 19, 2024 - The Java switch statement executes one statement from multiple conditions. It compares a given expression against a series of potential values (known as cases) and executes the code block corresponding to the first match.
🌐
Coderanch
coderanch.com › t › 720488 › java › switch-statements-Java
What are the new changes to switch statements in Java 13? (Java in General forum at Coderanch)
October 29, 2019 - In a switch statement, the expression must be an expression statement–an expression, which can be converted to a statement by adding a semicolon to it. In a switch expression, the expression may be any valid Java expression. The following snippet of code rewrites the previous example using a switch expression: This time, "One", "Two", etc.
🌐
Jenkov
jenkov.com › tutorials › java › switch.html
Java switch Statements
The Java switch statement provides a handy way to select a specific action based on the value of a given variable. From Java 12 the switch statement can even be used as an expression meaning it can return a value instead of only being able to ...
🌐
Simplilearn
simplilearn.com › home › resources › software development › what is switch case in java and how to use switch statement in java
What is Switch Case in Java and How to Use Switch Statement in Java | Simplilearn
September 10, 2025 - In java, the switch case is a multi-way branch statement that allows a variable to be tested against a list of values. Learn more about the switch case now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States