带字符串的switch case(在Java1.7中)如何在内部工作?
我有一个查询与switch-case-with-string有关,jvm在switch-case-with-string(特性在java 1.7中出现)的情况下如何在内部工作?switch语句将其表达式中的string对象与每个case标签关联的表达式进行比较,就好像它使用了string.equals方法一样;因此,switch语句中字符串对象的比较区分大小写 Java开关大小写使用String.equals()方法将传递的值与大小写值进行比较 根据Switch中字符串的Java7文档,Java编译器从使用字符串对象的Switch语句生成的字节码通常比从链接的if-then-else语句生成的字节码更高效 请参见此示例:带字符串的switch case(在Java1.7中)如何在内部工作?,java,switch-statement,conditional,Java,Switch Statement,Conditional,我有一个查询与switch-case-with-string有关,jvm在switch-case-with-string(特性在java 1.7中出现)的情况下如何在内部工作?switch语句将其表达式中的string对象与每个case标签关联的表达式进行比较,就好像它使用了string.equals方法一样;因此,switch语句中字符串对象的比较区分大小写 Java开关大小写使用String.equals()方法将传递的值与大小写值进行比较 根据Switch中字符串的Java7文档,Java
String fruit ="Mango";
switch (fruit) {
case "Apple": System.out.println("It's Apple : "+"Apple".hashCode());
break;
case "mango": System.out.println("It's mango : "+"mango".hashCode());
break;
case "Mango": System.out.println("It's Mango : "+"Mango".hashCode());
break;
}
JVM将其转换如下:
String fruit = "Mango";
String str1;
switch ((str1 = fruit).hashCode()) {
case 63476538:
if (str1.equals("Apple")) {
System.out.println("It's Mango : " + "Mango".hashCode());
}
break;
case 74109858:
if (str1.equals("Mango")) {
System.out.println("It's Mango : " + "Mango".hashCode());
}
break;
case 103662530:
if (!str1.equals("mango")) {
System.out.println("It's mango : " + "mango".hashCode());
return;
}
break;
}
switch语句将其表达式中的String对象与每个case标签关联的表达式进行比较,就像它使用String.equals方法一样;因此,switch语句中字符串对象的比较区分大小写 Java开关大小写使用String.equals()方法将传递的值与大小写值进行比较 根据Switch中字符串的Java7文档,Java编译器从使用字符串对象的Switch语句生成的字节码通常比从链接的if-then-else语句生成的字节码更高效 请参见此示例:
String fruit ="Mango";
switch (fruit) {
case "Apple": System.out.println("It's Apple : "+"Apple".hashCode());
break;
case "mango": System.out.println("It's mango : "+"mango".hashCode());
break;
case "Mango": System.out.println("It's Mango : "+"Mango".hashCode());
break;
}
JVM将其转换如下:
String fruit = "Mango";
String str1;
switch ((str1 = fruit).hashCode()) {
case 63476538:
if (str1.equals("Apple")) {
System.out.println("It's Mango : " + "Mango".hashCode());
}
break;
case 74109858:
if (str1.equals("Mango")) {
System.out.println("It's Mango : " + "Mango".hashCode());
}
break;
case 103662530:
if (!str1.equals("mango")) {
System.out.println("It's mango : " + "mango".hashCode());
return;
}
break;
}
阅读本文:阅读本文:我想知道,JVM是否使用了哈希概念?@taruntygi是的,它使用哈希。选中编辑的Anwsery您也应该显示一个带有默认大小写的示例。我想知道,JVM是否使用了哈希概念?@taruntygi是的,它使用哈希。选中编辑的Anwsery您也应该显示一个带有默认大小写的示例。