What is the difference between the String#equals
method and the String#contentEquals
method?
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.