switch情况下的Java字符串示例

136 阅读3分钟

Java7版本已经引入了在switch情况下使用string

在Java7版本之前,Switch case 只允许Integer,byte,short,char 变量,以及enumeration 值。不允许对String的支持。

这一特性便于开发者在switch case中直接使用字符串,并简化了开发者的可读性。

让我们看看下面的代码。

public class SwitchWithStringsDemo {
    public static void main(String[] args) {
        int days = 1;
        String s="one";
        String numberValue;
        switch (s) {
            case 1:  numberValue = "ONE";
                break;
            case 2:  numberValue = "TWO";
                break;
            case 3:  numberValue = "THREE";
                break;
            default: numberValue = "Invalid number";
                break;
        }

    }
}  

如果我们在eclipse/net beans中使用Java 1.7版本编译上述程序,会发生什么?让我们看看上述程序在命令行中的输出?

在JDK7版本之前,在switch情况下使用String的Eclipse错误

如果我们在switch case ,直接使用String ,eclipse /net beans代码编辑器会抛出以下错误。

Can not switch on a value of type string for source-level below 1.7. Only convertible int values or enum constants are permitted.

当在switch中使用字符串的java程序被编译为以下java7版本时,在命令行中抛出了以下错误。

线程 "main "中出现异常 java.lang.RuntimeException。无法编译的源代码 - 开关中的字符串在-source 1.6 中不被支持(使用-source 7或更高版本来启用开关中的字符串)。

在Java 7中使用开关中的字符串案例功能的优势。

Switch case 的一个替代方案是if else 语句,但它的可读性不高,同时也会使开发人员感到困惑,因为它的代码行数更多,循环复杂性也会增加。

循环复杂性是一种识别代码复杂性的方法。

我们有不同的替代方法来使用在Strings .

Java 1.7以下的Switch案例中使用if-else的字符串的替代方法。

它不完全等同于String在Switch情况下的用法,而是切换情况下的一种替代方法。

public class IfElseDemo {
    public static void main(String[] args) {
        String value = "ONE";
        if ("ONE".equals(value)) {
            System.out.println("Message One");
        }
        if ("TWO".equals(value)) {
            System.out.println("Message One");
        }
        if ("THREE".equals(value)) {
            System.out.println("Message One");
        }
    }
}

使用枚举在Switch情况下替代String的例子

在Java 7之前,枚举可以被映射到switch case中,下面的代码说明了枚举的用法。

public class AlternativeToStringSwitch {

    public enum Daily {
        ONE, TWO, THREE;
    }
    public static void main(String args[]) {
        String str = "ONE";
        switch (Daily.valueOf(str)) {
            case ONE:
                System.out.println("one");
                break;
            case TWO:
                System.out.println("two");
                break;
            case THREE:
                System.out.println("three");
                break;
        }
    }
}

java7中开关情况下的字符串例子

下面是一个关于java7中开关盒字符串教程的例子

public class SwitchStringDemoDemo {
    public static void main(String[] args) {
        int days = 1;
        String string="one";
        String numberValue;
        switch (string) {
            case "one":  numberValue = "ONE";
                break;
            case "two":  numberValue = "TWO";
                break;
            case "three":  numberValue = "THREE";
                break;
            default: numberValue = "Invalid number";
                break;
        }

    }
}  

在Java7中,字符串的变量被分配并可以在switch case中被引用。在jdk6版本中,案例中的字符串值是常数。

在java7中处理Switch情况下的字符串的空值检查

和其他Java代码一样,如果在Switch case中把null作为String传递,NullPointerException
我们必须处理对传递的字符串的空值检查。

总结

本教程讲述了在java7的switch情况下使用字符串的问题,还包括了在java7版本之前的替代方案和例子。