JAVA7 switch 新特性

One of these changes is a "String in a switch". In the previous versions of Java, the argument of switch had to be only of the following primitive data types: byte, short, char, int, or enum. Starting from JDK 7, you can use arguments of type String in the expression of a switch statement.


package switchtest;

public class SwitchTest {

public static void main(String[] args) {

String color = "red";
String colorRGB;
switch (color.toLowerCase()) {
case "black": colorRGB = "000000"; break;
case "red": colorRGB = "ff0000"; break;
case "green": colorRGB = "008000"; break;
case "blue": colorRGB = "0000ff"; break;
default: colorRGB = "Invalid color"; break;
}
System.out.println(colorRGB);
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章