String Comparison

image

Just a reminder about String comparisons. Compare two Strings like this:

if ( s1.equals(s2) )

not like this:

if (s1 == s2)

The first compares string contents, the second, string addresses (this is another artifact of reference types, remember?). Failing to use equals() to compare two strings is probably the most common single mistake made by Java novices.

Get Just Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.