String.equals() in Java compares the content (character sequence) of two strings, returning true if they are identical and false otherwise. It is the standard method for checking if two strings have the same value.

  • Syntax: public boolean equals(Object anotherObject)

  • Key Points:

    • Compares character-by-character, ignoring object memory location.

    • Returns false if the string is null (safe from NullPointerException).

    • Case-sensitive; use equalsIgnoreCase() for case-insensitive comparison.

    • Always preferred over == for content comparison, as == checks reference equality (same object in memory).

Example:

String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(str1.equals(str2)); // true (same content)
System.out.println(str1 == str2);     // false (different objects)

For robust comparison, especially when null values are possible, use Objects.equals(str1, str2) (available from JDK 7).

== 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 Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate · ❮ String Methods · 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 true because they are equal System.out.println(myStr1.equals(myStr3)); // false ·
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
Discussions

Java string equals string in if statement
Lee B is having issues with: Hey gang, I'm getting a very strange error, or lack of error rather, in a bit of code that seemingly looks fine. The goal is to print "f... More on teamtreehouse.com
🌐 teamtreehouse.com
2
April 2, 2019
How does Java string compare work, and when should you use `==` vs. `.equals()`? - TestMu AI Community
I’ve been using the == operator to compare strings in my program, but I encountered a bug. After switching to .equals(), the issue was resolved. What is the difference between == and .equals() when comparing strings in Java? Is using == for string comparison a bad practice, and in what scenarios ... More on community.testmuai.com
🌐 community.testmuai.com
0
March 23, 2025
Java. Is there a String that is not equal to itself?

This may crash or may not, depending on how the constructor of B is defined. Maybe the constructor of B sets name to a random word every time the constructor is called or maybe it's always the same.

More on reddit.com
🌐 r/learnprogramming
4
0
September 16, 2012
Opposite of .equals in Java?
!"stuff1". equals ("stuff2") More on reddit.com
🌐 r/NoStupidQuestions
2
1
November 3, 2021
🌐
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

🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - Note that this method does not take locale into account, and will result in an unsatisfactory ordering for certain locales. The java.text package provides collators to allow locale-sensitive ordering. ... a negative integer, zero, or a positive integer as the specified String is greater than, equal ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-equals-method-in-java
Java String equals() Method - GeeksforGeeks
July 23, 2025 - String equals() method in Java compares the content of two strings. It compares the value's character by character, irrespective of whether two strings are stored in the same memory location.
🌐
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 - Getting to know the String class in java took me some time. In general both the equals() method and “==” operator in Java are used to compare objects to check equality but the ‘’==’ operator’ checks if both objects point to the same memory location whereas the equals() method evaluates the values inside the objects.
Find elsewhere
🌐
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.
🌐
Codecademy
codecademy.com › docs › java › strings › .equals()
Java | Strings | .equals() | Codecademy
August 27, 2025 - The .equals() method returns true if two strings are equal in value. Otherwise, false is returned. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - This method compares two Strings character by character, ignoring their address. It considers them equal if they are of the same length and the characters are in same order:
🌐
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
🌐
Medium
medium.com › @thilinajayawardana_85346 › java-string-nullpointerexception-safe-equals-check-404481934e9b
Java String NullPointerException safe equals check | by Thilina Jayawardana | Medium
June 30, 2020 - Java String NullPointerException safe equals check If you compare two Strings in Java, normally you would use the equals method to compare them to see if they are similar. This will be some common …
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › equals
Java String equals() - Compare Strings Equality
December 23, 2024 - The equals() method in the String class is the primary tool for comparing the text of two strings, ensuring character-by-character equality without considering object identity in memory.
🌐
Programiz
programiz.com › java-programming › library › string › equals
Java String equals()
Become a certified Java programmer. Try Programiz PRO! ... The equals() method returns true if two strings are identical and false if the strings are different.
🌐
TestMu AI Community
community.testmuai.com › ask a question
How does Java string compare work, and when should you use `==` vs. `.equals()`? - TestMu AI Community
March 23, 2025 - I’ve been using the == operator to compare strings in my program, but I encountered a bug. After switching to .equals(), the issue was resolved. What is the difference between == and .equals() when comparing strings in …
🌐
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 - To properly compare the contents of two strings in Java, we should use the equals() method provided by the String class. The equals() method compares the actual characters in the strings and returns true if they are equal, false otherwise.
🌐
LinkedIn
linkedin.com › pulse › string-comparison-java-methods-performance-saeed-anabtawi-
String Comparison in Java: Methods and Performance Implications
June 15, 2023 - The equals() method is a part of the Object class in Java and is often overridden in subclasses like String. When used with strings, equals() checks if the content of two strings is identical.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string equals()
Java String equals()
November 23, 2023 - Java String equals() method compares two strings according to their contents.
🌐
Simplilearn
simplilearn.com › home › resources › software development › the ultimate tutorial on string comparison in java
The Ultimate Tutorial on String Comparison in Java
September 14, 2025 - You can compare string in java on the basis of content and reference. Learn all about string comparison in Java with several examples. Start learning!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Scaler
scaler.com › home › topics › difference between comparing string using == and .equals() method in java
Difference between Comparing String Using == and .equals() Method in Java - Scaler Topics
June 2, 2024 - The equals() method in Java compares two objects for equality. It is defined in the Object class in Java. The equals() method compares characters by characters and matches the same sequence present or not in both objects.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java string equals method
Java String equals Method
September 1, 2008 - If the given string value is the same as the specified object value, the equals() method returns true. In the following program, we are instating the String class with the value "Java".