CLASSES AND METHODS
Java String equals() method
equals() compares the text contents of two String objects. If the character sequences match, it returns true; otherwise, false.
Comparing strings
String pirmasVardas = "Rūta";
String antrasVardas = "Rūta";
String treciasVardas = "Mantas";
System.out.println(pirmasVardas.equals(antrasVardas));
System.out.println(pirmasVardas.equals(treciasVardas));true
false
pirmasVardasandantrasVardasstore the same text, Rūta.equals()compares their letters in order, so the first result istrue.treciasVardasstores the text Mantas.- Its contents differ, so the second comparison returns
false.
Method syntax
String class defines the method as follows:
public boolean equals(Object anotherObject)
| Part | Meaning |
|---|---|
| Calling object | pirmasVardas in the line pirmasVardas.equals(...) |
| Parameter | anotherObject – the other object to compare |
| Return type | boolean |
| Result | truewhen the contents match; otherwise, false |
The parameter has the Object type because the equals() method originates in the common Object class itself. String overrides it to compare character sequences.
equals() and == are different
String pirmas = new String("Labas");
String antras = new String("Labas");
System.out.println(pirmas == antras);
System.out.println(pirmas.equals(antras));false
true
- Both
new String("Labas")calls create separate objects. pirmas == antrasasks whether both references point to the same object. They do not, givingfalse.pirmas.equals(antras)compares the two objects' text.- Both strings are Labas, so
equals()returnstrue.
| Property | == operator | equals() method |
|---|---|---|
| Purpose | Compares primitive values or object references | Compares logical object equality |
| Used with | Primitive types and objects | Objects only |
| Object comparison | Checks whether references point to the same object | Can check object contents |
| Primitive type comparison | Compares actual values | Not applicable |
| Returns | boolean | boolean |
| Can it be overridden? | No | Yes |
| Default object behavior | Compares references | Object.equals() compares references if the class has not overridden it |
| String comparison | Compares references | Compares character sequences |
| Best use | Primitive values or deliberate identity checks | Logical object or string equality |
Use == with primitive types
int pirmas = 12;
int antras = 12;
System.out.println(pirmas == antras);true
int is not an object and has no methods, so you cannot write pirmas.equals(antras). Here, == directly compares two numeric values.
Why does == sometimes return true?
String a = "kava";
String b = "kava";
String c = new String("kava");
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));true
false
true
Java can reuse identical string literals from the string pool, so a and b may refer to the same object. But identical text obtained elsewhere or created with new may be a separate object. Do not compare string contents with == : use equals().
Letter case matters
String ivestasMiestas = "vilnius";
System.out.println(ivestasMiestas.equals("Vilnius"));
System.out.println(ivestasMiestas.equalsIgnoreCase("Vilnius"));false
true
equals() is case-sensitive, so vilnius does not equal Vilnius. When you deliberately want to ignore case, use equalsIgnoreCase().
Safe comparison with null
String atsakymas = null;
System.out.println("taip".equals(atsakymas));
// atsakymas.equals("taip") would cause a NullPointerExceptionfalse
A method cannot be called through a null reference. If one value is a known string literal, call the method on it: "taip".equals(atsakymas). If both values might be null, use Objects.equals(pirmas, antras).
What should you remember?
String.equals()compares a string's character sequence.==with objects checks whether references point to the same object.==is suitable for primitive values.equals()returnsbooleanvalue.equals()is case-sensitive.- Do not call the method on a potentially
nullreference.