Why can't I use switch statement on a String?

前端 未结 16 2016
面向向阳花
面向向阳花 2020-11-21 07:57

Is this functionality going to be put into a later Java version?

Can someone explain why I can\'t do this, as in, the technical way Java\'s switch state

相关标签:
16条回答
  • 2020-11-21 08:08

    JEP 354: Switch Expressions (Preview) in JDK-13 and JEP 361: Switch Expressions (Standard) in JDK-14 will extend the switch statement so it can be used as an expression.

    Now you can:

    • directly assign variable from switch expression,
    • use new form of switch label (case L ->):

      The code to the right of a "case L ->" switch label is restricted to be an expression, a block, or (for convenience) a throw statement.

    • use multiple constants per case, separated by commas,
    • and also there are no more value breaks:

      To yield a value from a switch expression, the break with value statement is dropped in favor of a yield statement.

    So the demo from the answers (1, 2) might look like this:

      public static void main(String[] args) {
        switch (args[0]) {
          case "Monday", "Tuesday", "Wednesday" ->  System.out.println("boring");
          case "Thursday" -> System.out.println("getting better");
          case "Friday", "Saturday", "Sunday" -> System.out.println("much better");
        }
    
    0 讨论(0)
  • 2020-11-21 08:08

    When you use intellij also look at:

    File -> Project Structure -> Project

    File -> Project Structure -> Modules

    When you have multiple modules make sure you set the correct language level in the module tab.

    0 讨论(0)
  • 2020-11-21 08:08
    public class StringSwitchCase { 
    
        public static void main(String args[]) {
    
            visitIsland("Santorini"); 
            visitIsland("Crete"); 
            visitIsland("Paros"); 
    
        } 
    
        public static void visitIsland(String island) {
             switch(island) {
              case "Corfu": 
                   System.out.println("User wants to visit Corfu");
                   break; 
              case "Crete": 
                   System.out.println("User wants to visit Crete");
                   break; 
              case "Santorini": 
                   System.out.println("User wants to visit Santorini");
                   break; 
              case "Mykonos": 
                   System.out.println("User wants to visit Mykonos");
                   break; 
             default: 
                   System.out.println("Unknown Island");
                   break; 
             } 
        } 
    
    } 
    
    0 讨论(0)
  • 2020-11-21 08:11

    It's a breeze in Groovy; I embed the groovy jar and create a groovy utility class to do all these things and more which I find exasperating to do in Java (since I am stuck using Java 6 in the enterprise.)

    it.'p'.each{
    switch (it.@name.text()){
       case "choclate":
         myholder.myval=(it.text());
         break;
         }}...
    
    0 讨论(0)
  • 2020-11-21 08:14

    An example of direct String usage since 1.7 may be shown as well:

    public static void main(String[] args) {
    
        switch (args[0]) {
            case "Monday":
            case "Tuesday":
            case "Wednesday":
                System.out.println("boring");
                break;
            case "Thursday":
                System.out.println("getting better");
            case "Friday":
            case "Saturday":
            case "Sunday":
                System.out.println("much better");
                break;
        }
    
    }
    
    0 讨论(0)
  • 2020-11-21 08:17

    In Java 11+ it's possible with variables too. The only condition is it must be a constant.

    For Example:

    final String LEFT = "left";
    final String RIGHT = "right";
    final String UP = "up";
    final String DOWN = "down";
    
    String var = ...;
    
    switch (var) {
        case LEFT:
        case RIGHT:
        case DOWN:
        default:
            return 0;
    }
    

    PS. I've not tried this with earlier jdks. So please update the answer if it's supported there too.

    0 讨论(0)
提交回复
热议问题