带有 String 的 Java 7 switch 语句是否使用 equals() 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18635869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:42:58  来源:igfitidea点击:

Does Java 7 switch statement with String use equals() method?

javastringswitch-statementequals

提问by Narendra Pathai

Java 7 supports switching with Stringslike the code below

Java 7 支持使用Strings如下代码切换

switch (month.toLowerCase()) {
case "january":
    monthNumber = 1;
    break;
case "february":
    monthNumber = 2;
    break;
default: 
    monthNumber = 0;
    break;
}

Does Java call the equals()method on each Stringcase? Or it relies on ==or intern()?

Java 是否equals()在每种String情况下调用该方法?或者它依赖于==intern()

Is this simply equivalent to:

这是否仅相当于:

String month = month.toLowerCase();
if("january".equals(month)){
monthNumber = 1;
}else if("february".equals(month)){
monthNumber = 1;
}..

UPDATE:

更新:

The String in the switch expression is compared with the expressions associated with each case label as ifthe String.equalsmethod were being used.

switch 表达式中的 String 与与每个 case 标签关联的表达式进行比较,就好像String.equals正在使用该方法一样。

As the docs point out that the behavior is as if equals()is called.

正如文档指出的那样,行为就像equals()被调用一样。

采纳答案by Aniket Thakur

The docssay

文件说,

The String in the switch expression is compared with the expressions associated
with each case label as if the String.equals method were being used.

Since it says as ifmy guess would be it does not though the internal implementation would be the same as .equals() method.

因为它说好像我的猜测是它不是虽然内部实现与 .equals() 方法相同。

回答by Juned Ahsan

The switchstatement when used with a Stringuses the equals()method to compare the given expression to each value in the casestatement and is therefore case-sensitive and will throw a NullPointerExceptionif the expression is null.

switch与使用时声明String使用equals()方法为给定的表达比较在每个值case语句,并因此区分大小写和将抛出NullPointerException如果表达式null

回答by Ben Dale

Yes.

是的。

"The switch statement when used with a String uses the equals() method to compare the given expression to each value in the case statement and is therefore case-sensitive and will throw a NullPointerException if the expression is null."

“与 String 一起使用的 switch 语句使用 equals() 方法将给定的表达式与 case 语句中的每个值进行比较,因此区分大小写,如果表达式为 null,则会抛出 NullPointerException。”

http://java.dzone.com/articles/new-java-7-feature-string

http://java.dzone.com/articles/new-java-7-feature-string

回答by Steinar

The Java 7 switch statement actually generates bytecode that uses both the hashCode()and equals()methods. The hash code is used to generate faster switch lookups; i.e. to avoid a chain of equals checks like you would get with an if-else chain.

Java 7 switch 语句实际上生成使用hashCode()equals()方法的字节码。哈希码用于生成更快的开关查找;即避免像使用 if-else 链那样进行一系列 equals 检查。

回答by Saurab Parakh

I found a useful aticle that explains how switch over string is implemented in Java 7

我发现了一个有用的文章,它解释了如何在 Java 7 中实现字符串切换