Switch可否用string作参数

jdk 1.7 以前,switch 只支持 int 、short、byte、char 类型,其实 switch 只支持 int 类型,可是 short 、 byte 、char 能够自动转化到 int 。精度小的向精度大的转化。code

jdk 1.7 以后,switch 即可以支持 String、枚举类型、Boolean 类型,其实 jdk 1.7 没有没有新的指令处理 switch String,而是调用了switch 中String.hashcode 方法,将 String 转化为 int 。 hash

相关文章
相关标签/搜索