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);
        }
    }

你可能感兴趣的:(jdk,java7)