Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. String.equals() / equalsIgnoreCase() / compareTo()

String.equals() / equalsIgnoreCase() / compareTo()

Methods for comparing whether two strings are equal or for comparing their lexicographic order. In Java, the == operator compares object references, so always use equals() to compare the contents of strings.

Syntax

// Checks whether two strings have the same content.
string.equals(Object anObject);

// Compares two strings, ignoring case differences.
string.equalsIgnoreCase(String anotherString);

// Compares strings lexicographically and returns an integer indicating the difference.
string.compareTo(String anotherString);

// Compares strings lexicographically, ignoring case differences.
string.compareToIgnoreCase(String str);

Method List

MethodDescription
equals(Object anObject)Returns true if the string contents are equal, or false otherwise.
equalsIgnoreCase(String anotherString)Compares two strings while ignoring case differences. Returns true if they are equal.
compareTo(String anotherString)Compares strings lexicographically. Returns 0 if equal, a negative integer if the calling string comes first, or a positive integer if it comes after.
compareToIgnoreCase(String str)Compares strings lexicographically, ignoring case differences.

Sample Code

// Use equals() to compare string contents.
String a = "Java";
String b = "Java";
System.out.println(a.equals(b)); // Prints "true".

// == compares references and may produce unexpected results.
String c = new String("Java");
System.out.println(a == c);      // Prints "false".
System.out.println(a.equals(c)); // Prints "true".

// Use equalsIgnoreCase() to compare strings without regard to case.
System.out.println("JAVA".equalsIgnoreCase("java")); // Prints "true".

// Check for null before calling equals() to avoid a NullPointerException.
String input = null;
System.out.println("Java".equals(input)); // Prints "false". (Avoids NullPointerException)

// Use compareTo() to compare strings lexicographically.
System.out.println("apple".compareTo("banana")); // Prints a negative value. ("apple" comes before "banana")
System.out.println("Java".compareTo("Java"));    // Prints "0".

// compareTo() can be used as a sorting criterion.
java.util.List<String> langs = new java.util.ArrayList<>(java.util.Arrays.asList("Python", "Java", "C", "Swift"));
langs.sort(String::compareTo);
System.out.println(langs); // Prints the list sorted in lexicographic order.

Notes

When comparing strings in Java, always use equals() instead of ==. == compares object references (memory addresses), so two strings with identical content created with new String() will return false. Using == with string literals may coincidentally return true due to the string pool mechanism, but you should not rely on this behavior intentionally.

If there is any chance the calling object is null, calling null.equals(...) will throw a NullPointerException. To avoid this, place the string literal on the left side (e.g., "Java".equals(input)) or use Objects.equals(a, b).

To convert string case, use toUpperCase() / toLowerCase(). To convert between strings and numbers, use String.valueOf().

If you find any errors or copyright issues, please .