开发者社区> 问答> 正文

Java 中 switch 对字符串支持中如何实现的?

Java 中 switch 对字符串支持中如何实现的?

展开
收起
vncamyi27xznk 2021-11-12 19:49:36 452 0
1 条回答
写回答
取消 提交回答
  • "public class switchDemoString { public static void main(String[] args) { String str = ""world""; switch (str) { case ""hello"": System.out.println(""hello""); break; case ""world"": System.out.println(""world""); break; default: break; } } }

    对代码进行反编译: public class switchDemoString { public switchDemoString() { } public static void main(String args[]) { String str = ""world""; String s; switch((s = str).hashCode()) { default: break; case 99162322: if(s.equals(""hello"")) System.out.println(""hello""); break; case 113318802: if(s.equals(""world"")) System.out.println(""world""); break; } } } 看到这个代码,你知道原来字符串的 switch 是通过 equals()和 hashCode()方法来实现的。 这本电子书收录于开发者藏经阁,下载链接:https://developer.aliyun.com/topic/download?id=923"

    2021-11-12 22:20:47
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载