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

前端 未结 16 2286
面向向阳花
面向向阳花 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条回答
  •  萌比男神i
    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; 
             } 
        } 
    
    } 
    

提交回复
热议问题