在Java7之前,switch只能匹配整数值,和字符;而Java7添加了字符串的匹配特性。
代码如下:
1 package blog; 2 3 4 public class Main { 5 public static void show(String str) { 6 switch (str) { 7 case "people": 8 System.out.println("Input string is people"); 9 break; 10 case "school": 11 System.out.println("Input string is school"); 12 break; 13 case "bird": 14 System.out.println("Input string is bird"); 15 break; 16 default: 17 System.out.println("Others"); 18 } 19 } 20 21 public static void main(String[] args) { 22 show("people"); 23 show("bird"); 24 show("dfkdf"); 25 } 26 }
Java7 switch新特性,布布扣,bubuko.com
原文:http://www.cnblogs.com/wss-is-knight/p/3627094.html