Popular Tutorials
Start Learning JavaCreated with over a decade of experience.
Certification Courses
Created with over a decade of experience and thousands of feedback.
Java String Methods
- Java String split()
- Java String compareTo()
- Java String compareToIgnoreCase()
- Java String length()
- Java String replace()
- Java String replaceAll()
- Java String substring()
- Java String equals()
- Java String equalsIgnoreCase()
- Java String contains()
- Java String indexOf()
- Java String trim()
- Java String charAt()
- Java String toLowerCase()
- Java String concat()
- Java String valueOf()
- Java String matches()
- Java String startsWith()
- Java String endsWith()
- Java String isEmpty()
- Java String intern()
- Java String getBytes()
- Java String contentEquals()
- Java String hashCode()
- Java String join()
- Java String replaceFirst()
- Java String subSequence()
- Java String toCharArray()
- Java String format()
Java String contentEquals()
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()
class Main {
public static void main(String[] args) {
String str = "Java";
String str1 = "Java";
StringBuffer sb1 = new StringBuffer("Java");
CharSequence cs1 = "Java";
String str2 = "JavA";
StringBuffer sb2 = new StringBuffer("JavA");
CharSequence cs2 = "JavA";
System.out.println(str.contentEquals(str1)); // true
System.out.println(str.contentEquals(sb1)); // true
System.out.println(str.contentEquals(cs1)); // true
System.out.println(str.contentEquals(str2)); // false
System.out.println(str.contentEquals(sb2)); // false
System.out.println(str.contentEquals(cs2)); // false
}
}
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.
