switch (x)
{
case A:
case B:
case C:
.doSomething()
}
Is there a way I could have those 3 cases in one line? For example something like this
case A, B, C:
?

- 85,076
- 16
- 154
- 332

- 323
- 2
- 9
- 21
-
What's wrong with what you have? – Reimeus Nov 04 '13 at 05:06
-
whatever u have written that will do the same as u want. – Android Killer Nov 04 '13 at 05:06
-
2Yes: `case A: case B: case C:`, the three in the **same** line. – Luiggi Mendoza Nov 04 '13 at 05:08
-
@Reimeus Nothing really I'm just curious if it's possible. For example when declaring integer values, you could have `int x` `int y` `int z`etc. or you could have `int x, y, z` I'm just wondering if that's possible with cases. – Oscar F Nov 04 '13 at 05:08
-
http://stackoverflow.com/questions/5086322/java-switch-statement-multiple-cases – Bishnu Rawal Nov 04 '13 at 05:16
3 Answers
Aside from removing the line breaks and putting those case
s all on the same line, no.
You will have to have three case
keywords and three :
's.
If you want details, see section 14.11 in the JLS. In particular:
SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :
There is no pattern in the grammar that would accept something like case A,B,C:
for a SwitchLabel.
It is a common practice, though, to structure cases as in your example when multiple cases do the same thing:
switch (value) {
case 1:
case 3:
case 5:
System.out.println("It's a positive odd number less than 7!");
break;
case 4:
case 8:
System.out.println("It's a multiple of 4 between 1 and 9!");
break;
default:
System.out.println("It's just another boring number.");
break;
}
Java programmers will generally get a clear understanding of code like that when they read it. Putting multiple cases on a single line (i.e. no line break) is much less common and will not be as clearly understood at a glance by a typical programmer (who would probably just think you accidentally deleted a line break).

- 38,729
- 14
- 126
- 182
Not the way you suggest, but you can do this (from the documentation):
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);
}
}

- 29,932
- 7
- 42
- 70
For example when declaring integer values, you could have int x int y int zetc. or you could have int x, y, z I'm just wondering if that's possible with cases.
That is a complete different question and it is possible depending on what you are doing.
You can have nested switches like
switch(x) {
case 1:
switch(y) {
case 2:
switch(z) {
case 3:
Or you can use a formula provided you know the range of possible values like this
switch(x * 100 + y * 10 + z) {
case 123: // x = 1, y = 2, z = 3
obviously this assumes x, y, z are between [0..9]

- 525,659
- 79
- 751
- 1,130