Check Anagram String In Java

In this article we will see what is anagram string and how to check if two strings are anagram or not.

If two strings are having same number of occurrences for each and every character then we can say that they are anagram. Here order in which character are present inside strings does not matter.

We will take two strings as example 1) listen 2) silent. Another example is heart and earth.

So lets get started with code.

import java.util.Arrays;

public class AnagramString {

	public static void main(String[] args) {
		String str1 = "listen";
		String str2 = "Silent";
		System.out.println(isAnagram(str1, str2));
	public static boolean isAnagram (String str1, String str2) {
		if (str1 == null || str2 == null) {
			return false;
		if (str1.length() != str2.length()) {
			return false;
		char[] charArray1 = str1.toLowerCase().toCharArray();
		char[] charArray2 = str2.toLowerCase().toCharArray();
		return new String(charArray1).equals(new String(charArray2));