== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they contain the same data).

Objects.equals() checks for null before calling .equals() so you don't have to (available as of JDK7, also available in Guava).

Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.equals().

// These two have the same value
new String("test").equals("test") // --> true 

// ... but they are not the same object
new String("test") == "test" // --> false 

// ... neither are these
new String("test") == new String("test") // --> false 

// ... but these are because literals are interned by 
// the compiler and thus refer to the same object
"test" == "test" // --> true 

// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true

// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true

From the Java Language Specification JLS 15.21.3. Reference Equality Operators == and !=:

While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters (§3.10.5, §3.10.6). The contents of two strings s and t can be tested for equality by the method invocation s.equals(t).

You almost always want to use Objects.equals(). In the rare situation where you know you're dealing with interned strings, you can use ==.

From JLS 3.10.5. String Literals:

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

Similar examples can also be found in JLS 3.10.5-1.

Other Methods To Consider

String.equalsIgnoreCase() value equality that ignores case. Beware, however, that this method can have unexpected results in various locale-related cases, see this question.

String.contentEquals() compares the content of the String with the content of any CharSequence (available since Java 1.5). Saves you from having to turn your StringBuffer, etc into a String before doing the equality comparison, but leaves the null checking to you.

🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-compareto-method-with-examples
Java String compareTo() Method with Examples - GeeksforGeeks
January 20, 2025 - The Java compareTo() method compares the given string with the current string lexicographically. It returns a positive number, a negative number, or 0.
🌐
Programiz
programiz.com › java-programming › library › string › compareto
Java String compareTo()
The compareTo() method takes the letter case (uppercase and lowercase) into consideration. class Main { public static void main(String[] args) { String str1 = "Learn Java"; String str2 = "learn Java"; int result; // comparing str1 with str2
Top answer
1 of 16
6152

== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they contain the same data).

Objects.equals() checks for null before calling .equals() so you don't have to (available as of JDK7, also available in Guava).

Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.equals().

// These two have the same value
new String("test").equals("test") // --> true 

// ... but they are not the same object
new String("test") == "test" // --> false 

// ... neither are these
new String("test") == new String("test") // --> false 

// ... but these are because literals are interned by 
// the compiler and thus refer to the same object
"test" == "test" // --> true 

// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true

// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true

From the Java Language Specification JLS 15.21.3. Reference Equality Operators == and !=:

While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters (§3.10.5, §3.10.6). The contents of two strings s and t can be tested for equality by the method invocation s.equals(t).

You almost always want to use Objects.equals(). In the rare situation where you know you're dealing with interned strings, you can use ==.

From JLS 3.10.5. String Literals:

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

Similar examples can also be found in JLS 3.10.5-1.

Other Methods To Consider

String.equalsIgnoreCase() value equality that ignores case. Beware, however, that this method can have unexpected results in various locale-related cases, see this question.

String.contentEquals() compares the content of the String with the content of any CharSequence (available since Java 1.5). Saves you from having to turn your StringBuffer, etc into a String before doing the equality comparison, but leaves the null checking to you.

2 of 16
796

== tests object references, .equals() tests the string values.

Sometimes it looks as if == compares values, because Java does some behind-the-scenes stuff to make sure identical in-line strings are actually the same object.

For example:

String fooString1 = new String("foo");
String fooString2 = new String("foo");

// Evaluates to false
fooString1 == fooString2;

// Evaluates to true
fooString1.equals(fooString2);

// Evaluates to true, because Java uses the same object
"bar" == "bar";

But beware of nulls!

== handles null strings fine, but calling .equals() from a null string will cause an exception:

String nullString1 = null;
String nullString2 = null;

// Evaluates to true
System.out.print(nullString1 == nullString2);

// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));

So if you know that fooString1 may be null, tell the reader that by writing

System.out.print(fooString1 != null && fooString1.equals("bar"));

The following are shorter, but it’s less obvious that it checks for null:

System.out.print("bar".equals(fooString1));  // "bar" is never null
System.out.print(Objects.equals(fooString1, "bar"));  // Java 7 required
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-compareto-method-example
Java String compareTo() Method with examples
Both the strings are same, however one of the string is in uppercase and the other string is in lowercase. public class JavaExample { public static void main(String args[]) { //uppercase String str1 = "HELLO"; //lowercase String str2 = "hello";; System.out.println(str1.compareTo(str2)); } }
🌐
CodeGym
codegym.cc › java blog › strings in java › java string compareto() method
Java String CompareTo() Method
December 25, 2024 - The compareTo() method returns 2 exceptions. ClassCastException, if the object can not be compared then it returns this exception. NullPointerException, if the string is null then NullPointerException is thrown. class Main { public static void main(String[] args) { // declaring strings to be used in this example for Java string compareTo() method String str = "Java compareTo() method example"; String str1 = "Java compareTo() method example"; String str2 = "this is Java compareTo() method example"; String str3 = "Java CompareTo() Method Example"; String str4 = "a Java compareTo() method example
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › compareTo
Java String compareTo() - Compare Strings Alphabetically | Vultr Docs
May 15, 2025 - In this article, you will learn how to use the compareTo() method in Java to compare strings alphabetically.
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-strings-in-java
Compare two Strings in Java - GeeksforGeeks
July 11, 2025 - Example: Java · // Java Program ... } public static void main(String[] args) { String s1 = "Java"; String s2 = "Domain"; // Call the compare function int res = compare(s1, s2); System.out.println("" + res); } } Output ...
Find elsewhere
🌐
Medium
medium.com › @alxkm › java-string-comparison-a-complete-guide-44df959a756f
Java String Comparison — A Complete Guide | by Alex Klimenko | Medium
July 11, 2025 - For example, Objects.equals(str1, str2) will correctly return true even if both strings are null, and it won't throw a NullPointerException. This makes Objects.equals() especially useful in scenarios where either object might be null, improving ...
🌐
Tutorialspoint
tutorialspoint.com › home › java › java string compareto method
Java String compareTo Method
September 1, 2008 - public class Test { public static ... are not immutable"); int result = str1.compareTo( str2 ); System.out.println(result); result = str2.compareTo( str3 ); System.out.println(result); } }...
🌐
TechVidvan
techvidvan.com › tutorials › java-string-compareto-method
Java String compareTo() Method with Examples - TechVidvan
March 7, 2024 - Uppercase and lowercase characters are regarded differently by compareTo() since it is case-sensitive. NullPointerException will be thrown if either the invoking string or anotherString is null. If this object cannot be compared to the supplied object, a ClassCastException will occur. Whenever the provided object is null, a NullPointerException is thrown. Lexicographic Comparison: You can compare strings lexicographically (in a dictionary-style) with the compareTo() function.
🌐
Codecademy
codecademy.com › docs › java › strings › .compareto()
Java | Strings | .compareTo() | Codecademy
July 14, 2025 - Yes, it is. Uppercase letters have lower Unicode values than lowercase letters. For example, "Apple".compareTo("apple") returns a negative number. The .compareToIgnoreCase() method compares two strings lexicographically but ignores case differences.
🌐
Reddit
reddit.com › r/javahelp › understanding the compareto() method
r/javahelp on Reddit: Understanding the compareTo() method
August 8, 2020 -

From what I have been reading, the compareTo() method returns the difference of the Unicode numerical values of two Strings when they are compared with each other. For instance, the String "hello" when compared with the String "hello" returns an integer value of zero, since they both have exactly the same Unicode characters in them. Based on my understanding of this method, "hello" should return zero when compared to "olleh", because the two Strings have the exact same Unicode characters in them. Instead, though, I am getting integer value of 7 returned to the console. Can someone break this down a bit for me to help me understand it better? Thanks in advance. Here is my code:

String str1 = "hello";
String str2 = "olleh";
System.out.println(str1.compareTo(str2)); // 7

🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - String string1 = "using equals ignore case"; String string2 = "USING EQUALS IGNORE CASE"; assertThat(string1.equalsIgnoreCase(string2)).isTrue(); The compareTo() method returns an int type value and compares two Strings character by character ...
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java string compareto method with programming examples
Java String compareTo Method With Programming Examples
April 1, 2025 - In the above example, we have taken five input Strings and performed a basic comparison between them using the .compareTo() Java method. In the first comparison, we have ‘A’ greater than ‘G’ by 6 characters in the alphabet series, so it returns +6.
🌐
Mkyong
mkyong.com › home › java › java string compareto() examples
Java String compareTo() examples - Mkyong.com
January 24, 2022 - For different lengths comparison strings, it works the same. System.out.println("a".compareTo("ab")); // -1 System.out.println("a".compareTo("abc")); // -2 System.out.println("a".compareTo("abcd")); // -3 System.out.println("11".compareTo("112")); // -1 System.out.println("11".compareTo("1123")); // -2 System.out.println("11".compareTo("11234")); // -3 · The Java String compareToIgnoreCase() method compares two strings lexicographically, ignoring cases.
🌐
Scaler
scaler.com › home › topics › java string compareto() method
Java String compareTo() Method with Examples - Scaler Topics
May 10, 2023 - In the example above, when we compare an empty string with a non-empty string it returns a negative number which is nothing but the length of the non-empty string. Vice-versa, when we compare the non-empty string with the empty one, we get a positive number that is again the length of the non-empty string. Thus, using the compareTo() in Java we can find the length of the string by comparing with an empty string as it returns the length of the non-empty string with positive and negative signs as per the position of the non-empty string.
🌐
Sentry
sentry.io › sentry answers › java › how to compare strings in java
How to compare strings in Java | Sentry
public class Main { public static void main(String[] arg) { String str1 = "java"; String str2 = "Java"; System.out.println(str1.equalsIgnoreCase(str2)); } } ... If you want to find out if a String is “bigger” or “smaller” than another ...
🌐
How to do in Java
howtodoinjava.com › home › string › java string compareto()
Java String compareTo()
January 11, 2023 - string1 = string2 – ‘string1’ and ‘string2’ are equal. The compareTo() function takes one argument of type String. The second string is the String object itself, on which method is called.