Ad space (banner)
โ˜•Java Lessons
Lesson 64 / 68

Checking Whether Two Words Are Anagrams

This lesson covers checking whether two words are anagrams in Java, so you understand a technique for comparing strings by rearranging them. It's for anyone searching "Java anagram check implementation."

An anagram is a different word formed by rearranging the same letters (like "listen" and "silent"). You can check for one by rearranging both strings' characters and comparing whether they match.

The example converts a string to lowercase and then to a character array with a.toLowerCase().toCharArray(), sorts it with Arrays.sort(), then compares with Arrays.equals(). "listen" and "silent" share the same letters, so once sorted, they match.

A common early mistake is not accounting for uppercase/lowercase differences. The example unifies to lowercase with toLowerCase() before comparing โ€” skip this, and the same word gets judged as different just because of case.

This is a basic technique for comparing strings while ignoring their character order โ€” for longer strings, counting how many times each character appears is a faster way to check.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)