== 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.

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
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-strings-in-java
Compare two Strings in Java - GeeksforGeeks
July 11, 2025 - The String.equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string. This method returns true if the argument is not null and the contents of both the Strings are same ignoring case, else false.
🌐
W3Schools
w3schools.com › java › ref_string_equals.asp
Java String equals() Method
String myStr1 = "Hello"; String myStr2 = "Hello"; String myStr3 = "Another String"; System.out.println(myStr1.equals(myStr2)); // Returns true because they are equal System.out.println(myStr1.equals(myStr3)); // false ... The equals() method ...
🌐
Sentry
sentry.io › sentry answers › java › how to compare strings in java
How to compare strings in Java | Sentry
The Problem How do I compare strings in Java? The Solution To compare strings in Java for equality, you should use String.equals() . Output: If uppercase and…
🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
A value less than 0 is returned ... string (more characters). Tip: Use compareToIgnoreCase() to compare two strings lexicographyically, ignoring lower case and upper case differences....
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - The method returns true if two Strings are equal by first comparing them using their address i.e “==”. Consequently, if both arguments are null, it returns true and if exactly one argument is null, it returns false.
🌐
Programiz
programiz.com › java-programming › examples › compare-strings
Java Program to Compare Strings
public class CompareStrings { public ... we've used String constructor to create the strings. To compare these strings in Java, we need to use the equal() method of the string....
🌐
Team Treehouse
teamtreehouse.com › community › java-string-equals-string-in-if-statement
Java string equals string in if statement (Example) | Treehouse Community
April 2, 2019 - The goal is to print "first is equal to second" if firstExample is the same thing as secondExample. String firstExample = "hello"; String secondExample = "hello"; String thirdExample = "HELLO"; // this is what the lesson uses and probably is wanting us to use for this, and fails the check, without printing anything into the "output" tab if (firstExample.equals(secondExample)) { console.printf("first is equal to second"); System.exit(0); } // also does not work, === or == if (firstExample == secondExample) { }
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 23925 › can-i-compare-two-strings-using-if-statement
Can I compare two strings using 'if' statement? | Sololearn: Learn to code for FREE!
Use (str1.compare(str2) > 0) to compare Strings. compare returns -1 if str1 < str2. compare returns 0 if str1 is equals str2. And compare returns +1 if str1 > str2. Always check s1.compare(s2) with Zero, like: < 0, or > 0, or == 0. ... yes, ...
🌐
Codingzap
codingzap.com › home › blog – programming & coding articles › how to compare two strings in java?
How to Compare Two Strings in Java? - Codingzap
December 21, 2025 - To compare two string in Java using the equals() method, let us first create the string that we need to compare. In this case, we will be declaring three strings but we will compare two string objects at one time.
🌐
Medium
medium.com › @alxkm › java-string-comparison-a-complete-guide-44df959a756f
Java String Comparison — A Complete Guide | by Alex Klimenko | Medium
July 11, 2025 - The == operator compares references, not values. It checks whether two string references point to the same object in memory. String a = "Hello"; String b = "Hello"; String c = new String("Hello"); System.out.println(a == b); // true (same reference ...
🌐
Javatpoint
javatpoint.com › string-comparison-in-java
String Comparison in Java - javatpoint
String Comparison in java. There are the three ways to compare the strings. Let's see the three ways with suitable examples.
🌐
Opensource.com
opensource.com › article › 19 › 9 › compare-strings-java
How to compare strings in Java | Opensource.com
... == is an operator that returns true if the contents being compared refer to the same memory or false if they don't. If two strings compared with == refer to the same string memory, the return value is true; if not, it is false.
🌐
Edureka
edureka.co › blog › comparing-two-strings-in-java
Comparing Two Strings In Java | String Comparison In Java | Edureka
June 6, 2023 - There are various methods to compare two strings in java, as seen below. The strings are compared on the basis of the values present in the string. The method returns true if the values of the two strings are same, and false, if ...
🌐
Scaler
scaler.com › home › topics › java › string comparison in java
String Comparison in Java - Scaler Topics
July 26, 2023 - In Java, the String.equals() method compares two strings based on the sequence of characters present in both strings. The method is called using two strings and returns a boolean value.
🌐
Delft Stack
delftstack.com › home › howto › java › java if statement string
How to Compare String With the Java if Statement | Delft Stack
February 12, 2024 - The first if statement (str1 == str2) compares two string literals, and since string literals are interned in Java, both str1 and str2 refer to the same object in memory.
🌐
CodingBat
codingbat.com › doc › java-string-equals-loops.html
Java String Equals and Loops
There is a variant of equals() called equalsIgnoreCase() that compares two strings, ignoring uppercase/lowercase differences. String a = "hello"; String b = "there"; if (a.equals("hello")) { // Correct -- use .equals() to compare Strings } if (a == "hello") { // NO NO NO -- do not use == with Strings } // a.equals(b) -> false // b.equals("there") -> true // b.equals("There") -> false // b.equalsIgnoreCase("THERE") -> true
🌐
JavaBeat
javabeat.net › home › how to compare strings in java [14 different methods]
How to Compare Strings in Java [14 Different Methods]
March 20, 2024 - Users can compare strings in Java using two ways, i.e., “string reference” comparison and “string content” comparison. When comparing by reference, Java checks if the two strings are stored in the exact same memory location.
🌐
Cogentuniversity
cogentuniversity.com › post › string-comparison-in-java
String Comparison in Java
Within the main method, we call compareStrings and provide two strings, "Java" and "Java", for comparison. The function internally invokes the equals() method to ascertain equality and returns the result, which is then printed. Using user-defined functions in this manner not only enhances code modularity but also facilitates the implementation of custom comparison rules, if ...