🌐
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.
Discussions

Switch Case in Java, never use it?
No. A switch statement is generally easier to read than repeated else if statements, plus it's less work for you. Of course this only pertains to certain arguments, so you don't end up using it that often. To take off points for it based on personal preference? Ridiculous. Doesn't sound like the best professor to me. More on reddit.com
🌐 r/learnprogramming
60
34
April 27, 2012
Java switch use case - Stack Overflow
I'm reluctant to use a switch, but I saw switch will be improved in Java 12 Java 12 added the switch expression as an experimental feature. A Java switch expression is a switch statement which can More on stackoverflow.com
🌐 stackoverflow.com
When to use switch case statements and when to use if, else if and else statements?
They are not exactly the same. switch...case is generally used when you have a limited, distinct set of values for everything else if...else More on reddit.com
🌐 r/learnprogramming
14
1
March 30, 2021
Do i have to use "switch" statements if i am comfortable with using "if" statements?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
38
37
May 12, 2022
People also ask

Can a switch statement be used inside another switch in Java?
Yes, Java allows nested switch statements. You can place one switch inside a case of another.
🌐
wscubetech.com
wscubetech.com › resources › java › switch-statement
Java Switch Statement/Case: Syntax, Examples, Flowchart
Does the Java switch statement support String values?
Yes, from Java 7 onwards, switch supports String values.
🌐
wscubetech.com
wscubetech.com › resources › java › switch-statement
Java Switch Statement/Case: Syntax, Examples, Flowchart
Is fall-through ever useful in Java switch statements?
Yes, it can be used intentionally for grouping multiple cases with the same result.
🌐
wscubetech.com
wscubetech.com › resources › java › switch-statement
Java Switch Statement/Case: Syntax, Examples, Flowchart
🌐
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.
🌐
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)
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.
🌐
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   3 weeks ago
🌐
WsCube Tech
wscubetech.com › resources › java › switch-statement
Java Switch Statement/Case: Syntax, Examples, Flowchart
September 2, 2025 - Explore Java’s switch statement with easy syntax, flowcharts, and code examples. Understand its rules, restrictions, applications, and more. Read now!
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › enhancements-for-switch-statement-in-java-13
Enhancements for Switch Statement in Java 13 - GeeksforGeeks
March 13, 2024 - The variables declared in the traditional switch exists until the end of the switch statement. If we want the variables to have a case level scope, we can use {} introduced by the enhanced switch in Java 13.
🌐
Code with C
codewithc.com › code with c › java tutorials › java switch statement: a comprehensive guide to conditional logic
Java Switch Statement: A Comprehensive Guide To Conditional Logic - Code With C
March 14, 2024 - Now, let’s uncover the mystical powers of the Switch Statement and how it can work wonders in your Java code: Imagine having a bunch of if-else statements cluttering your code like a messy room. The Switch Statement swoops in like a magical broom, sweeping away the chaos and organizing your logic into neat, distinct cases.
🌐
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.
🌐
Reddit
reddit.com › r/learnprogramming › switch case in java, never use it?
r/learnprogramming on Reddit: Switch Case in Java, never use it?
April 27, 2012 -

Should you never use a switch case in java? I'm in an intermediate programming course and the professor took points off my project because I used a switch case to get an int that corresponded to a different variable. He said switch cases are his pet peeve and to never use them.

Valid?

🌐
Study.com
study.com › courses › business courses › java programming tutorial & training
Switch Statement in Java: Example & Syntax | Study.com
A Java switch statement is like a request to a railroad switch operator; based on the command from the engineer, the train will go down a given track. In the Java statement, an expression is evaluated, and specific case statements are processed ...
🌐
Medium
medium.com › @manisha10850 › switch-statement-in-java-af83821fb4e2
Switch Statement in Java. In Java, a switch statement is used to… | by Manisha Shukla | Medium
May 28, 2025 - Switch Statement in Java In Java, a switch statement is used to make decisions based on the value of an expression. It provides a more concise way to write multiple if-else statements when you have a …
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.

🌐
nipafx
nipafx.dev › java-switch
How To Use switch In Modern Java // nipafx
April 19, 2022 - With a default branch, on the other hand, new cases are (silently) caught and processed by it. Java's switch allows us to pick the behavior that best fits each given situation. (NB: Check the section on patterns for more on exhaustiveness.) Some problems can only be solved reasonably with a switch statement.
🌐
JanBask Training
janbasktraining.com › community › java › java-switch-use-case
Java switch use case | JanBask Training Community
April 15, 2025 - The switch statement is a control flow tool that checks the value of a variable and executes code based on matching cases. ... int day = 2; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; ...
🌐
Scaler
scaler.com › home › topics › java › java switch statement
Java Switch Statement - Scaler Topics
February 16, 2024 - 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. Let us understand the usage of switch case in Java programmatically.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › language › switch-expressions-and-statements.html
9 Switch Expressions - Java
January 16, 2025 - Like all expressions, switch expressions evaluate to a single value and can be used in statements. They may contain "case L ->" labels that eliminate the need for break statements to prevent fall through. You can use a yield statement to specify the value of a switch expression.
🌐
Hero Vired
herovired.com › learning-hub › topics › switch-case-in-java
Switch Case in Java: Syntax, Examples, and Best Practices
A switch statement in Java is a control flow statement that allows the execution of one block of source code among many based on the value of an expression. It provides a more efficient and readable way to handle multiple possible values for ...
🌐
Refactoring.Guru
refactoring.guru › refactoring › smells
Code Smells
Switch Statements · Temporary Field · These smells mean that if you need to change something in one place in your code, you have to make many changes in other places too. Program development becomes much more complicated and expensive as a result. Divergent Change ·
🌐
Reddit
reddit.com › r/learnprogramming › when to use switch case statements and when to use if, else if and else statements?
r/learnprogramming on Reddit: When to use switch case statements and when to use if, else if and else statements?
March 30, 2021 -

I ask this question as I am learning Java and created a banking program. There are 5 options for a user to take, they select them by inputting "A","B", "C", "D" or "E" into the terminal - in this case, I feel switch case statements would be best to use.

There is a point where the user has to pick one of two options (create a account = "A" or log in with an existing account = "B"). As there are only two options, I feel using if statements would be fine here.

From your experience, when have you decided to opt for using switch case statements as opposed to if statements, and vice-versa? I would be really interested to hear examples of when you opted one over the other and your reasoning for doing so!