assertEquals
uses the equals
method for comparison. There is a different assert, assertSame
, which uses the ==
operator.
To understand why ==
shouldn't be used with strings you need to understand what ==
does: it does an identity check. That is, a == b
checks to see if a
and b
are the same object. It is built into the language, and its behavior cannot be changed by different classes. The equals
method, on the other hand, can be overridden by classes. Its default behavior (in the Object
class) is to do an identity check just like the ==
operator, but many classes, including String
, override it to instead do an equivalence check.
Analogy time: imagine that each String
object is a piece of paper with something written on it. Let's say I have two pieces of paper with "Foo" written on them, and another with "Bar" written on it. If I take the first two pieces of paper nad use ==
to compare them it will return false
because it's essentially asking "are these the same piece of paper?". It doesn't need to even look at what's written on the paper. The fact that I'm giving it two pieces of paper (rather than the same one twice) means it will return false
. If I use equals
, however, the equals
method will read the two pieces of paper ad see that they say the same thing ("Foo"), and so it'll return true
.
The bit that gets confusing with Strings is that the Java has a concept of "interning" Strings, and this is (effectively) automatically performed on any string literals in your code. This means that if you have two equivalent string literals in your code (even if they're in different classes) they'll actually both refer to the same String object. This makes the ==
operator return true
more often than one might expect.