I used java after a long time and found out that unlike other languages java doesn't support switch case with strings(only with no's and enums). What's the reason behind it? If other languages implement it easily, why not java?
Asked
Active
Viewed 1.5k times
3
-
possible duplicate of [Switch Statement with Strings in Java](http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java) – Wayne Jan 12 '12 at 20:53
-
Thanks for the link.. I googled searched this before asking this question.. but didn't find that link in my search.. – Ank Jan 12 '12 at 20:55
-
What language allows strings in switch statements? – Hot Licks Jan 12 '12 at 20:56
-
1@Ankur I very much doubt that C supports switching on strings, characters yes (so does Java). – Ben van Gompel Jan 12 '12 at 21:26
-
@Ankur How do you do it in C? – Fredrik Jan 12 '12 at 21:28
-
well yaa.. C supports characters not strings.. but Perl and VB does – Ank Jan 12 '12 at 21:29
-
Possible duplicate of [Why can't I switch on a String?](https://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string) – default locale Jun 01 '17 at 07:02
6 Answers
12
This feature was implemented in Java 7 (which was released in July this year). Why didn't they implement it earlier? Well J7 was really delayed because of the whole Sun acquisition by Oracle.
Latest documentation: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Mateusz Dymczyk
- 14,969
- 10
- 59
- 94
-
1as I said after my edit: the development of Java was really delayed because Sun was bought by Oracle. – Mateusz Dymczyk Jan 12 '12 at 20:55
-
@Zenzen I thought Oracle bought Java, not Sun, or are they the same thing? – fireshadow52 Jan 12 '12 at 20:59
-
No, Oracle bought the whole company - Sun and with it Java but not only (they got also, for example, MySQL). You can find all the info here: http://www.oracle.com/us/corporate/press/018363. It took place in 2009, the talks were probably (don't remember) going on for at least months if not years and Java 6 was released in 2006 -> Java 7 didn't have the time to be properly developed by Sun. – Mateusz Dymczyk Jan 12 '12 at 21:00
-
Personally, I don't recommend use of switch case along with `Strings` as it is really really inefficient. At the java byte code level, JVM invokes `java.lang.String.hashCode()` for the switch case. Since same value of strings can have same hash it then compared against char by char comparison using `java.lang.String.equals()`. – Govinnage Rasika Perera Feb 23 '15 at 04:41
-
@RasikaPerera what you're saying isn't wrong, I just don't think it would really matter in a real world application but on the other hand might make the code more readable for some people – Mateusz Dymczyk Feb 23 '15 at 04:58
-
@MateuszDymczyk partially agree with you. In that case, you could use `Enums` to have maintainable, readable and performant switch statement. – Govinnage Rasika Perera Feb 23 '15 at 06:08
5
IMO, the designers of Java were smart to leave out switching on Strings. Everytime you do a switch on a String (and, I admit, I do it sometimes too) you should be thinking:
- Why aren't I using polymorphism here?
- At the very least, should I be using an Enum instead?

user949300
- 15,364
- 7
- 35
- 66
-
completely agreed! ignore using switching on Strings as much as you can. Or rethink the design of your system. – Govinnage Rasika Perera Feb 23 '15 at 04:44
-
Switching on user input is more direct than converting it to an enum (using a switch maybe?) and then switching on that enum. It also makes it easier to dependency cycles, and it is more flexible. Besides, enums were only added in Java 5, so the original decision had nothing to do with them :) – Elazar Jun 01 '17 at 06:52
0
The new Java Version (JDK 7) takes long time to release. That's why you may not be able to know that.... Here is an example how to use strings in switch case...
option = scanner.nextLine();
switch (option) {
case "1": System.out.println("Hello");break;
case "2": break;
case "3": break;
case "4": break;
case "5": break;
default: System.out.println("Invalid option. Please try again..."); break;
}

Sankar
- 162
- 2
- 13
0
Not very pretty but here is another way for Java 6 and bellow:
String runFct =
queryType.equals("eq") ? "method1":
queryType.equals("L_L")? "method2":
queryType.equals("L_R")? "method3":
queryType.equals("L_LR")? "method4":
"method5";
Method m = this.getClass().getMethod(runFct);
m.invoke(queryField);

Conete Cristian
- 101
- 4