java.lang.String
) are pieces of text stored in Java programs.equals
or equalsIgnoreCase
methods.
Example:
String firstString = "Test123";
String secondString = "Test" + 123;
if (firstString.equals(secondString)) {
// Both Strings have the same content.
}
Example (ignoring case):
if (firstString.equalsIgnoreCase(secondString)) {
// Both Strings are equal, ignoring the case of the individual characters.
}
equalsIgnoreCase
doesn't let you specify a Locale, which might affect equality, e.g., in Turkish.toLowerCase(Locale)
or toUpperCase(Locale)
with equals
for locale-sensitive comparisons.
Example:
Locale locale = Locale.forLanguageTag("tr-TR");
System.out.println(firstString.toLowerCase(locale).equals(secondString.toLowerCase(locale)));
==
for String Comparison==
or !=
operators to compare Strings unless you can guarantee string internment.String.equals(Object)
method for accurate value-based comparison.NullPointerException
.String.equals
and are case-sensitive.
Example:
String stringToSwitch = "A";
switch (stringToSwitch) {
case "a":
System.out.println("a");
break;
case "A":
System.out.println("A");
break;
case "B":
System.out.println("B");
break;
default:
break;
}