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));
Console output

true
false

  1. pirmasVardas and antrasVardas store the same text, Rūta.
  2. equals() compares their letters in order, so the first result is true.
  3. treciasVardas stores the text Mantas.
  4. Its contents differ, so the second comparison returns false.

Method syntax

String class defines the method as follows:

public boolean equals(Object anotherObject)

Parts of the equals() method
PartMeaning
Calling objectpirmasVardas in the line pirmasVardas.equals(...)
ParameteranotherObject – the other object to compare
Return typeboolean
Resulttruewhen 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));
Console output

false
true

  1. Both new String("Labas") calls create separate objects.
  2. pirmas == antras asks whether both references point to the same object. They do not, giving false.
  3. pirmas.equals(antras) compares the two objects' text.
  4. Both strings are Labas, so equals() returns true.
Differences between the == operator and equals() method
Property== operatorequals() method
PurposeCompares primitive values or object referencesCompares logical object equality
Used withPrimitive types and objectsObjects only
Object comparisonChecks whether references point to the same objectCan check object contents
Primitive type comparisonCompares actual valuesNot applicable
Returnsbooleanboolean
Can it be overridden?NoYes
Default object behaviorCompares referencesObject.equals() compares references if the class has not overridden it
String comparisonCompares referencesCompares character sequences
Best usePrimitive values or deliberate identity checksLogical object or string equality

Use == with primitive types

int pirmas = 12;
int antras = 12;

System.out.println(pirmas == antras);
Console output

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));
Console output

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"));
Console output

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 NullPointerException
Console output

false

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() returns boolean value.
  • equals() is case-sensitive.
  • Do not call the method on a potentially null reference.