49

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.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120

5 Answers5

47

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.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • Is there any other mechanism which a compiler could use which wouldn't rely upon details of string implementation which future implementations would be allowed to change, other than perhaps pre-checking reference equality before the virtual method call [and perhaps even before hashing?] – supercat Dec 19 '13 at 22:18
13

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.

Steinar
  • 5,860
  • 1
  • 25
  • 23
  • Does it ever test for reference equality before computing the hash code? I would guess that it would be pretty common that a switch value would be reference-equal to one of the switch labels. – supercat Dec 19 '13 at 22:20
  • No, I don't think so. It seems to compute the hashcode first, jump to section with String's for that hashcode and then check equals on those Strings (usually only one). – Steinar Dec 21 '13 at 10:59
  • Interesting. I would think that strings given to a `switch` statement would be literals more than half the time, although I guess the hashing could still be a win *if* either a hash code has been computed already *or* it will be needed in future. – supercat Dec 21 '13 at 16:11
7

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."

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

mawburn
  • 2,232
  • 4
  • 29
  • 48
Ben Dale
  • 2,492
  • 1
  • 14
  • 14
  • 3
    Because some random blog post says it does not necessarily mean it is correct. – cowls Sep 05 '13 at 12:16
  • 1
    I did actually research a fair bit on this subject, my answer just contained one of the resources I used. – Ben Dale Sep 05 '13 at 12:23
6

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.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

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

Saurab Parakh
  • 1,054
  • 3
  • 12
  • 19