Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
Bad practice or frowned upon to use a comma to separate cases? Switch case statement Java

is it considered bad practice to write a switch case statement with a comma such as this:

switch(name)
{
case 'a', 'A' :
break;
}

Rather than

switch(name)
{
case 'a':
case 'A':
break;
}

Just curious as my code seems to run fine either way but I want to get into the habit of using the proper/most accepted way.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

It's not bad practice. In fact, it's considered concise and time-efficient.

Take a look at the following code that you have

switch(name){
    case 'a', 'A' :
        break;
}

The equivalent without using commas would be:

switch(name){
    case 'a':
        break;
    case 'A':
        break;
}

And the if-else equivalent would be:

if(name=='a'){
    //Do something
}else if(name=='A'){
    //Do something
}

Of these, the first example took a mere 36 characters to type. The latter took 49, and the last one took 37 characters.

Moral? Using the comma is definitely more concise and time-efficient that using two cases with a single result. Even the if statements would be more concise.

over 4 years ago · Santiago Trujillo Report

0

CheckStyle, the defacto standard for java style, has no check for this.

Do whatever is easiest to read.

I would say though use only one style or the other in any given switch statement.

over 4 years ago · Santiago Trujillo Report

0

From JavaDocs: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

class SwitchDemo2 {
    public static void main(String[] args) {

        int month = 2;
        int year = 2000;
        int numDays = 0;

        switch (month) {
            case 1: case 3: case 5:
            case 7: case 8: case 10:
            case 12:
                numDays = 31;
                break;
            case 4: case 6:
            case 9: case 11:
                numDays = 30;
                break;
            case 2:
                if (((year % 4 == 0) && 
                     !(year % 100 == 0))
                     || (year % 400 == 0))
                    numDays = 29;
                else
                    numDays = 28;
                break;
            default:
                System.out.println("Invalid month.");
                break;
        }
        System.out.println("Number of Days = "
                           + numDays);
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!