Java7 switch新特性

在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 }
View Code

 

posted @ 2014-03-26 21:51  瓦尔登湖畔的小木屋  阅读(462)  评论(1编辑  收藏  举报