在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从而进行判断。

相关文章:

  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-20
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2021-10-13
相关资源
相似解决方案