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

前端 未结 16 2024
面向向阳花
面向向阳花 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: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;
        }
    
    }
    

提交回复
热议问题