🌐
W3Schools
w3schools.com › java › ref_string_equals.asp
Java String equals() Method
The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically. ... If you want to use W3Schools services as an educational institution, ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-equals-method-in-java
Java String equals() Method - GeeksforGeeks
July 23, 2025 - We compare three strings using the equalsIgnoreCase() method of str1, str2, and str3 to check case-insensitive comparison. ... // Working equalsIgnoreCase() Method import java.io.*; public class Geeks { public static void main(String[] args) { // Define three strings String str1 = "Hello"; String str2 = "hello"; String str3 = "WORLD"; // Comparison between str1 and str2 System.out.println("str1.equals(str2): "+ str1.equals(str2)); // Comparison between str1 and str2(Case Insensitive) System.out.println("str1.equalsIgnoreCase(str2): "+ str1.equalsIgnoreCase(str2)); // Comparison between str2 and str3 System.out.println("str2.equalsIgnoreCase(str3): "+ str2.equalsIgnoreCase(str3)); } }
🌐
W3Schools
w3schools.com › java › ref_string_contentequals.asp
Java String contentEquals() Method
The StringBuffer class is like a String, only it can be modified, found in the java.lang package. The CharSequence interface is a readable sequence of char values, found in the java.lang package. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
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 ...
🌐
W3Schools Blog
w3schools.blog › home › string comparison in java
String comparison in java - W3schools
August 27, 2014 - String comparison in java: In java there are three ways to compare two strings. 1. By == operator. 2. By equals() method. 3. By compareTo() method.
🌐
W3Schools
w3schools.in › java › examples › compare-two-strings
Java Program to Compare Two Strings - W3Schools
This Java program is used to demonstrates a comparison of two strings. Java equals() method is used to compare strings.
🌐
Cach3
w3schools.com.cach3.com › java › ref_string_equals.asp.html
Java String equals() Method - W3Schools
Tip: Use the compareTo() method to compare two strings lexicographically. ... Tabs Dropdowns Accordions Side Navigation Top Navigation Modal Boxes Progress Bars Parallax Login Form HTML Includes Google Maps Range Sliders Tooltips Slideshow Filter List Sort List · HTML CSS JavaScript SQL Python ...
🌐
W3Schools Blog
w3schools.blog › home › java string comparison
Java String comparison - W3schools
August 27, 2014 - In Java, there are three ways to compare two strings. By == operator. By equals() method.
🌐
Programiz
programiz.com › java-programming › library › string › equals
Java String equals()
The equals() method takes a single argument. ... 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
Find elsewhere
🌐
TechVidvan
techvidvan.com › tutorials › java-string-equals-method
Java String equals() Method - TechVidvan
March 18, 2024 - The String equals() function in Java compares two strings based on their data/content.
🌐
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
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
🌐
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!
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java string equals method
Java String equals Method
September 1, 2008 - The equals() method accepts an object as a parameter which is comparable. It does not throw any exception while comparing or checking whether the string is equal to the specified object or not.
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-strings-in-java
Compare two Strings in Java - GeeksforGeeks
July 11, 2025 - Explanation: In the above example, the equals() method checks the content of s1 and s2. Since they differ, it returns false.It returns true for s1 and s3 because they have identical content. ... We can define our own function to compare strings lexicographically.
🌐
W3Schools
w3schools.com › java › ref_string_equalsignorecase.asp
Java String equalsIgnoreCase() 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 myStr1 = "Hello"; String myStr2 = "HELLO"; String myStr3 = "Another String"; System.out.println(myStr1.equalsIgnoreCase(myStr2)); // true System.out.println(myStr1.equalsIgnoreCase(myStr3)); // false
🌐
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…
🌐
CodeGym
codegym.cc › java blog › strings in java › java string equals()
Java String equals()
November 23, 2023 - String myString1 = new String ("here is my favorite string"); String myString2 = new String ("here is my favorite string"); doesn’t fall into the String Pool, but becomes a separate object, even if its text completely matches the same string from the String Pool. Moreover, if we compare strings using String equals() method, it will check not the address, but the contents of the string, the sequence of characters in the strings.