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?

7
  • possible duplicate of Switch Statement with Strings in Java Commented Jan 12, 2012 at 20:53
  • Thanks for the link.. I googled searched this before asking this question.. but didn't find that link in my search.. Commented Jan 12, 2012 at 20:55
  • What language allows strings in switch statements? Commented Jan 12, 2012 at 20:56
  • 1
    @Ankur I very much doubt that C supports switching on strings, characters yes (so does Java). Commented Jan 12, 2012 at 21:26
  • @Ankur How do you do it in C? Commented Jan 12, 2012 at 21:28

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

6 Comments

as I said after my edit: the development of Java was really delayed because Sun was bought by Oracle.
@Zenzen I thought Oracle bought Java, not Sun, or are they the same thing?
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: 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.
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().
@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
|
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?

2 Comments

completely agreed! ignore using switching on Strings as much as you can. Or rethink the design of your system.
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 :)
2

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

Comments

2

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

Comments

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;
  }

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.