当前位置:首页 > 开发 > 编程语言 > Java > 正文

JAVA7 switch 新特性

发表于: 2013-04-23   作者:stdayong   来源:转载   浏览:
摘要: 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
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);
        }
    }

JAVA7 switch 新特性

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号