Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda