3

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?

Ank
  • 6,040
  • 22
  • 67
  • 100

6 Answers6

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
  • 1
    as 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:

  1. Why aren't I using polymorphism here?
  2. 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
2

You can use Strings in switch statements as of Java 7.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
2

As of java 7 you can use switch case with strings.. see: java switch case

AAaa
  • 3,659
  • 7
  • 35
  • 40
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);