Java如何在switch语句中使用字符串?
从Java7版本开始,您现在可以String在switch语句中使用。在以前的版本中,我们只能用常量型的byte,char,short,int(以及它们相应的参考/包装型)或enum在常量switch声明。
下面的代码为您提供了有关Java7如何扩展以允许使用Stringinswitch语句的简单示例。
package org.nhooo.example.basic;
public class StringInSwitchExample {
public static void main(String[] args) {
String day = "Sunday";
switch (day) {
case "Sunday":
System.out.println("doSomething");
break;
case "Monday":
System.out.println("doSomethingElse");
break;
case "Tuesday":
case "Wednesday":
System.out.println("doSomeOtherThings");
break;
default:
System.out.println("doDefault");
break;
}
}
}
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短