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

相關文章
相關標籤/搜索