switch能否用string做参数

在Java5以前,switch(expr)中,exper只能是byte,short,char,int类型。

从Java5开始,java中引入了枚举类型,即enum类型。

从Java7开始,exper还可以是String类型。

但是long在所有版本中都是不可以的。

为什么jdk1.7后又可以用string类型作为switch参数呢?

其实,jdk1.7并没有新的指令来处理switch string,而是通过调用switch中string.hashCode,将string转换为int从而进行判断。

原文地址:https://www.cnblogs.com/feicheninfo/p/9660824.html