0

I am using switch{case ()} with String in java, and so far it seems to be working perfectly. But I want to confirm before continuing too much; is this safe?

I mean, I know that I should not be doing if ("hi" == "hi"), but should be doing "hi".equals ("hi") because String refers to a reference point...

I am not too sure how switch works, and I have not been able to confirm from any of my sources...

Thank you!!

Orange
  • 1

1 Answers1

0

According to the language specification (emphasis mine):

If one of the case constants is equal to the value of the expression, then we say that the case matches, and all statements after the matching case label in the switch block, if any, are executed in sequence.

This implies that the switch case is evaluated using equals, not ==.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243