首页 > 技术文章 > Switch能否使用String做参数

hupp 2015-09-29 16:06 原文

在Java语言中Swith可以使用参数类型有:Only convertible int values, strings or enum variables are permitted

可以自动转换为整型的(byte,short,int),String类型,枚举类型。

Java中不能做为Switch参数的有boolean,float,double,long(不能直接转换为int啊)。

public class Main {
    public static void main(String[] args) {
        String s="abc";
        switch(s){
        case("abc"):
            System.out.println("i love xingxing");
            break;
        case("123"):
            System.out.println("i love jiming");
            break;
        default:
            break;
        }
    }
}

 

推荐阅读