Difference between String#equals and String#contentEquals methods

Arathana picture Arathana · Jun 25, 2011 · Viewed 59.2k times · Source

What is the difference between the String#equals method and the String#contentEquals method?

Answer

BalusC picture BalusC · Jun 25, 2011

The String#equals() not only compares the String's contents, but also checks if the other object is also an instance of a String. The String#contentEquals() only compares the contents (the character sequence) and does not check if the other object is also an instance of String. It can be anything as long as it is an implementation of CharSequence which covers a.o. String, StringBuilder, StringBuffer, CharBuffer, etc.