Running on Java 17+35-2724 (Preview)
Home of The JavaSpecialists' Newsletter

1.16 isAnagram() Method

Juppies 2 by Dr Heinz M. Kabutz

The isAnagram() method converts the two Strings to character arrays. A String is made up of individual characters. A character would be, for example 'c', 'h', 'o', etc., whereas a String would be a sequence of characters, such as "chocolate". Note that the quotation marks are different. For a String we use the double-quotation mark " and for the char (short for character) we use the single-quotation mark '. Each char uses two bytes to store the letter, allowing many scripts, including Greek, Cyrillic, Japanese, Chinese (simplified), etc. In modern Java versions, the String compresses the chars into a single bytes, in order to save space. In Java 8, the String "Good Morning, Vietnam" uses 88 bytes, but in Java 11 it is only 64 bytes. This can make an impact with systems that have a lot of Strings (which would be most systems).

To determine whether the two Strings contain the exact same characters, we simply sort both arrays with the method Arrays.sort() and then compare whether they are equal with Arrays.equals(). This won't show us where the differences are, but we can hopefully eyeball that easily enough.

Comments

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Table of Contents

We hope you enjoyed this tutorial. If you did, you will also enjoy our courses. We suggest you start with Extreme Java - Advanced Java, followed by Extreme Java - Concurrency Performance for Java 8.



About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack 21

Superpack 21 Our entire Java Specialists Training in one huge bundle more...

Free Java Course

Free Juppies 2 Course
Juppies 2 - a course for complete beginners more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...

Java Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.