Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › Strings, StringBuilder & String Pool

Compare equals(), ==, equalsIgnoreCase(), and compareTo() for Strings, and what's the locale gotcha?

Level II · JuniorStrings, StringBuilder & String Pool
Short answer

== compares references (true only for the same object, e.g., two literals), equals() compares content code-unit by code-unit, equalsIgnoreCase() folds ASCII-ish case, and compareTo() gives lexicographic ordering by UTF-16 value for sorting.

Explanation

The gotcha is case and ordering are not locale-correct: toUpperCase()/toLowerCase() and natural ordering can be wrong for non-English locales (the Turkish dotless-i is the famous example), so use Locale-aware methods or a Collator for human-facing comparison and sorting.

Common mistakes
  • Using == to compare string content.
  • Calling toUpperCase() without a Locale (the Turkish 'i' problem).

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Strings, StringBuilder & String Pool.

Practise free

More on Strings, StringBuilder & String Pool