STRING INSTANCE METHODS
Java String contentEquals() method
contentEquals() compares a String string with another character sequence. It is especially useful for comparing an immutable String with a mutable StringBuilder or StringBuffer.
Comparing text contents
String tekstas = "Java kursas";
StringBuilder kuriamasTekstas = new StringBuilder("Java kursas");
System.out.println(tekstas.contentEquals(kuriamasTekstas));
kuriamasTekstas.append(" internetu");
System.out.println(tekstas.contentEquals(kuriamasTekstas));true
false
tekstasstores the immutable text Java kursas.kuriamasTekstasis a separateStringBuilderobject, but initially contains the same characters.contentEquals()compares characters rather than object types or references, so the first result istrue.append()appends this text to the builder: internetu.- The sequences now differ, so the second comparison returns
false.
Method syntax and overloads
String class has two contentEquals() overloads:
public boolean contentEquals(StringBuffer sb)public boolean contentEquals(CharSequence cs)
| Part | Meaning |
|---|---|
| Calling object | tekstas in the call tekstas.contentEquals(...) |
| Parameter | StringBuffer or the more general CharSequence |
| Return type | boolean |
true | The lengths and all characters at corresponding positions match |
false | At least one character or the sequence length differs |
| Error | NullPointerExceptionwhen the argument is null |
StringBuilder, StringBuffer and String implement CharSequence, so they can be passed as the sequence to compare.
Why is contentEquals() an instance method?
contentEquals() checks a specific String object's contents, so it is called on that string:
boolean sutampa = tekstas.contentEquals(builder);
On the left, tekstas is the base character sequence, while builder is passed as an argument. We therefore do not write String.contentEquals(...) : the method needs a specific String instance whose characters it can compare.
Comparing StringBuilder and StringBuffer
String kodas = "JAVA-21";
StringBuilder builder = new StringBuilder("JAVA-21");
StringBuffer buffer = new StringBuffer("JAVA-21");
System.out.println(kodas.contentEquals(builder));
System.out.println(kodas.contentEquals(buffer));true
true
kodas,builderandbufferare objects of different types.- All three objects contain the same seven characters, JAVA-21.
- The first call accepts
StringBuilderas aCharSequence. - The second call accepts
StringBuffer. - In both cases, the lengths, characters and order match, giving
true.
The difference between contentEquals() and equals()
String tekstas = "Labas";
StringBuilder builder = new StringBuilder("Labas");
System.out.println(tekstas.equals(builder));
System.out.println(tekstas.contentEquals(builder));false
true
String.equals() returns true only when the compared object is also a String with matching text. contentEquals() supports different CharSequence implementations: it can compare String and StringBuilder by their characters.
| Situation | Suitable method |
|---|---|
Two String objects | Usually equals() |
String and StringBuilder | contentEquals() |
String and StringBuffer | contentEquals() |
| Letter case must be ignored | This method does not support that; for two String use equalsIgnoreCase() |
Character order must match
String tekstas = "Java";
StringBuilder builder = new StringBuilder("avaJ");
System.out.println(tekstas.contentEquals(builder));false
Both sequences contain the same letters, but their positions differ. contentEquals() checks the exact sequence from first character to last, so Java and avaJ are not equal.
Uppercase and lowercase letters differ
String tekstas = "JavaQuest";
StringBuffer buffer = new StringBuffer("javaquest");
System.out.println(tekstas.contentEquals(buffer));false
contentEquals() has no case-insensitive variant. J and j are different characters, so JavaQuest and javaquest do not match.
Comparing empty sequences
String tuscias = "";
StringBuilder tusciasBuilder = new StringBuilder();
StringBuilder netusciasBuilder = new StringBuilder("Java");
System.out.println(tuscias.contentEquals(tusciasBuilder));
System.out.println(tuscias.contentEquals(netusciasBuilder));true
false
Two empty strings have the same length and no differing characters, so they are equal. When the builder contains Java, the lengths differ and the method returns false.
A null argument causes an error
String tekstas = "Java";
CharSequence lyginamas = null;
System.out.println(tekstas.contentEquals(lyginamas));NullPointerException
lyginamasis declared as aCharSequence, but its value isnull.contentEquals()must read the sequence's length and characters.nullobject does not exist, so comparison is impossible and it throwsNullPointerException.
Checking safely before comparison
String tekstas = "Java";
CharSequence lyginamas = null;
boolean sutampa = lyginamas != null
&& tekstas.contentEquals(lyginamas);
System.out.println(sutampa);false
First, the code checks lyginamas != null. Since it is false, the short-circuit operator && skips the second condition, so contentEquals() is not called and no error occurs.
Common mistakes
- Using
equals()expectingStringandStringBuilderwith the same text to be equal. - Expecting
contentEquals()to ignore uppercase and lowercase differences. - Passing a
nullargument, producing aNullPointerException. contentEquals()is confused withcontains(): the first checks entire-content equality; the second searches for a substring.
When should you use contentEquals()?
Use contentEquals()when you have a String and want to compare its contents directly with a StringBuilder, StringBuffer or another CharSequence. If both values are String, it is usually clearer to use equals(). To find only part of a string, use contains().