首页 > 代码库 > 【java】switch case支持的7种数据类型

【java】switch case支持的7种数据类型

switch表达式后面的数据类型只能是byte,short,char,int四种整形类型,枚举类型和java.lang.String类型(从java 7才允许),不能是boolean类型。

在网上看到好多文章,说switch还支持byte,short,char,int 的包装类,首先可以肯定说switch不支持这些包装类,但是如下的代码又是正确的:

    public static void main(String[] args) {        switch (new Integer(45)) {        case 40:            System.out.println("40");            break;        case 45:            System.out.println("45");//将会打印这句            break;        default:            System.out.println("?");            break;        }    }

可以打印正确的结果,在挨着挨着试完Byte,Short,Character,Integer后发现都可以正确打印,于是便说switch也支持byte,short,char,int的包装类。这种说法不完全正确,之所以switch能够支持他们的包装类,是因为自动拆箱(就是自动将引用数据类型转化为基本数据类型)的原因,下面使用jclasslib软件打开上面的.class文件,

 1 0 new #2 <java/lang/Integer>                             创建一个Integer类的对象 2 3 dup                                                    将对象的标识压入栈顶部 3 4 bipush 45                                              将整形45压入栈中 4 6 invokespecial #3 <java/lang/Integer.<init>>            调用Integer类型的构造方法 5 9 invokervirtual #4 <java/lang/Integer.intValue>         调用intValue()方法 6 12 lookupswitch 2 7         40:40(+28) 8         45:51(+39) 9         defalut:62(+50)10 40 getstatic #5 <java/lang/System.out>                   获得标准输出流11 43 ldc #6 <40>                                           从常量池中将40的索引压入栈中12 45 invokevirtual #7 <java/io/PrintStream.println>        调用println()方法13 48 goto 70 (+22)14 51 gestatic #5 <java/lang/System.out>15 54 ldc #8 <45>16 56 invokevirtual #7 <java/io/PrintStream.println>17 59 goto 70 (+11)18 62 getstatic #5 <java/lang/System.out>19 65 ldc #9<?>20 67 invokevirtual #7 <java/io/PrintStream.println>21 70 return

从上面的第5行我们可以看出编译器自动调用了intValue()方法,如果是使用Byte会自动调用byteValue()方法,如果是Short会自动调用shortValue()方法,如果是Integer会自动调用intValue()方法。switch 的查找原理是使用key-offset在目标表格中查找的,lookupswitch后面的数字和goto后面的数字都是有规律的,关于更多信息可以查看The Java® Virtual Machine Specification

因此switch表达式后面的数据类型只能是byte,short,char,int四种整形类型,枚举类型和java.lang.String类型。

【java】switch case支持的7种数据类型