Java 7 supports switching with Strings
like the code below
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 String
case? Or it relies on ==
or 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 if the
String.equals
method were being used.
As the docs point out that the behavior is as if equals()
is called.
The docs say
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 if my guess would be it does not though the internal implementation would be the same as .equals() method.