The syntax of the string contentEquals() method is:
string.contentEquals(StringBuffer sb)
string.contentEquals(charSequence cs)
Here, string is an object of the String class.
contentEquals() Parameters
The contentEquals() method takes a single parameter.
- either
StringBufferorcharSequence
Note: You can pass any class that implements charSequence to the contentEquals() method. For example: String, StringBuffer, CharBuffer etc.
contentEquals() Return Value
- Returns
trueif the string contains the same sequence of characters as the specified parameter. If not, returnsfalse.
Example: Java String contentEquals()
Java String equals() Vs contentEquals()
The Java String equals() method not only compares the content, but also checks if the other object is an instance of String. However, contentEquals() only compares the content. For example,
class Main {
public static void main(String[] args) {
String str1 = "Java";
StringBuffer sb1 = new StringBuffer("Java");
System.out.println(str1.equals(sb1)); // false
System.out.println(str1.contentEquals(sb1)); // true
}
}
Here, both str1 and sb1 have the same content but they are instance of different objects. Hence, str1.equals(sb1) returns false and str1.contentEquals(sb1) returns true.