== 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_equals.asp
Java String equals() Method
Java Examples Java Videos Java ... · Compare strings to find out if they are equal: String myStr1 = "Hello"; String myStr2 = "Hello"; String myStr3 = "Another String"; System.out.println(myStr1.equals(myStr2)); // Returns ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-equals-method-in-java
Java String equals() Method - GeeksforGeeks
July 23, 2025 - The String equals() method overrides the equals() method of the object class. ... class Geeks { public static void main(String[] args) { String str1 = "Learn Java"; String str2 = "Learn Java"; String str3 = "Learn Kotlin"; boolean result; // Comparing str1 with str2 result = str1.equals(str2); System.out.println(result); // Comparing str1 with str3 result = str1.equals(str3); System.out.println(result); // Comparing str3 with str1 result = str3.equals(str1); System.out.println(result); } }
🌐
Codecademy
codecademy.com › docs › java › strings › .equals()
Java | Strings | .equals() | Codecademy
August 27, 2025 - Returns true if two strings are equal in value and false otherwise.
🌐
Programiz
programiz.com › java-programming › library › string › equals
Java String equals()
... class Main { public static void main(String[] args) { String str1 = "Learn Java"; String str2 = "Learn Java"; String str3 = "Learn Kolin"; boolean result; // comparing str1 with str2 result = str1.equals(str2); System.out.println(result); // true // comparing str1 with str3
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
797

== 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
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java string equals method
Java String equals Method
September 1, 2008 - The given string is: Java The object ... equals() method returns false. In the following example, we are creating a string literal with the value "HelloWorld"....
🌐
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 - 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 ...
🌐
TechVidvan
techvidvan.com › tutorials › java-string-equals-method
Java String equals() Method - TechVidvan
March 18, 2024 - Java String equals() compares two strings based on their data/content. It is used particularly to compare the contents of two String objects in the context of the String.
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › equals
Java String equals() - Compare Strings Equality
December 23, 2024 - This example demonstrates how equals() is used to validate user logins. It compares the password entered by the user against the stored, correct password. Accurate string matching is critical to ensure security and access control in applications.
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - Using the “==” operator for comparing text values is one of the most common mistakes Java beginners make. This is incorrect because “==” only checks the referential equality of two Strings, meaning if they reference the same object or not. Let’s see an example of this behavior: String string1 = "using comparison operator"; String string2 = "using comparison operator"; String string3 = new String("using comparison operator"); assertThat(string1 == string2).isTrue(); assertThat(string1 == string3).isFalse(); In the example above, the first assertion is true because the two variables point to the same String literal.
🌐
How to do in Java
howtodoinjava.com › home › string › java string.equals()
Java String.equals() with Examples - HowToDoInJava
January 9, 2023 - Java String equals() method example. Learn to compare Java strings using equals() method, equalsIgnoreCase() method and == operator.
🌐
Reddit
reddit.com › r/learnjava › can i use == in java for strings
r/learnjava on Reddit: Can I use == in Java for strings
October 8, 2024 -

My CS teacher told me that you have to use .equals() when comparing strings instead of == but today I tried using == and it worked. Was this supposed to happen or was it like a new update?

public class Main{
public static void main(String[] args) {
    
        String name = "jarod";

        if(name == "jarod"){
            System.out.println("ELLO JAROD");

        }else{
            System.out.print("NO");
        }

    }

}

For this bit of code the output was ello Jarod and if I put in something else for the string variable it gave me no

🌐
Javatpoint
javatpoint.com › java-string-equals
Java String equals()
Java String equals and equalsIgnoreCase methods with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string equals in java etc.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string equals()
Java String equals()
November 23, 2023 - At the same time, equals checks ... public static void main(String[] args) { String myString1 = "here is my favorite string"; String myString2 = "here is my favorite string"; //this string is the same as the previous one, ...
🌐
CodingBat
codingbat.com › doc › java-string-equals-loops.html
Java String Equals and Loops
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
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-strings-in-java
Compare two Strings in Java - GeeksforGeeks
July 11, 2025 - // Java Program to compare two ... method public class CompareStrings { public static void main(String[] args) { String s1 = "Hello"; String s2 = "Geeks"; String s3 = "Hello"; // Comparing strings System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); } } ... Explanation: ...
🌐
Reddit
reddit.com › r/learnjava › why to never use == with strings and instead use .equals()
r/learnjava on Reddit: Why to never use == with Strings and instead use .equals()
August 9, 2020 -

In class we were discussing this and basically I understood == tests the EXACT same thing... since Strings are each their own object, we need a special method to check for if they are actually the same words. Furthermore, == tests the address I believe?

Here's the confusing part for me... Java has optimized == so SOMETIMES "hello" == "hello" will return true even if they are two unique Strings... but "hell" += "o" == "hello" returns false. Why ?

🌐
Medium
medium.com › @oguzkaganbati › why-should-we-use-equals-method-instead-operator-for-string-comparison-in-java-510fc50057cc
Why Should We Use “equals()” Method Instead “==” Operator for String Comparison in Java? | by Oğuz Kağan BATI | Medium
March 5, 2024 - By using the equals() method, we’re comparing the content of ‘str1’ and ‘str2’, which in this case is ‘hello’. Therefore, the output is true, indicating that the strings have the same content. In Java, when comparing strings, it’s crucial to use the equals() method instead of the ‘==’ operator.
🌐
Medium
medium.com › @alxkm › java-string-comparison-a-complete-guide-44df959a756f
Java String Comparison — A Complete Guide | by Alex Klimenko | Medium
July 11, 2025 - It returns true if both objects are equal in value or if both are null, and false if one is null and the other is not, or if their values differ. For example, Objects.equals(str1, str2) will correctly return true even if both strings are null, ...