You can use

str1.compareTo(str2);

If str1 is lexicographically less than str2, a negative number will be returned, 0 if equal or a positive number if str1 is greater.

E.g.,

"a".compareTo("b"); // returns a negative number, here -1
"a".compareTo("a"); // returns  0
"b".compareTo("a"); // returns a positive number, here 1
"b".compareTo(null); // throws java.lang.NullPointerException
Answer from Johan Sjöberg on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
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 › compare-two-strings-in-java
Compare two Strings in Java - GeeksforGeeks
July 11, 2025 - To compare two strings in Java, the most common method is equals().
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
🌐
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 = new String("java"); String str2 = new String("java"); System.out.println(str1 == str2); } } ... Here, the two strings have equal values but these values are not stored ...
🌐
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.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › comparestrings.html
Comparing Strings and Portions of Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. The String class has a number of methods for comparing strings and portions of strings.
🌐
Medium
medium.com › @alxkm › java-string-comparison-a-complete-guide-44df959a756f
Java String Comparison — A Complete Guide | by Alex Klimenko | Medium
July 11, 2025 - Comparing strings is a common operation in Java, but it often leads to confusion, especially between using == and .equals(). This article provides a detailed overview of different ways to compare strings in Java with comprehensive examples, including both case-sensitive and case-insensitive comparisons, lexicographical comparisons, and performance considerations. In Java, ==, .equals(), and .compareTo() are all used to compare values, but they serve different purposes and behave differently. The == operator compares object references, meaning it checks whether two variables point to the same object in memory.
Find elsewhere
🌐
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 - Suppose, the second string is less than the first string, then it will print some negative value. If the second string is greater than the first string then it will print a positive value.
🌐
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

🌐
Blogger
javarevisited.blogspot.com › 2012 › 03 › how-to-compare-two-string-in-java.html
How to compare two String in Java - String Comparison Example
This article is in continuation of my earlier post on String like Difference between String and StringBuffer in Java and How to replace String in java using regular expression etc. If you haven’t read them already you may find them useful and worth reading. equals()method compare two Strings for content equality.
🌐
Opensource.com
opensource.com › article › 19 › 9 › compare-strings-java
How to compare strings in Java | Opensource.com
Which one should you use? As a common practice, use String equals for case-sensitive strings and String equalsIgnoreCase for case-insensitive comparisons. However, one caveat: take care of NPE (NullPointerException) if one or both strings are null.
🌐
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
🌐
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.
🌐
Scaler
scaler.com › home › topics › java › string comparison in java
String Comparison in Java - Scaler Topics
July 26, 2023 - If the first string is less than the second string, a negative result is returned. In Java, the String.equals() method compares two strings based on the sequence of characters present in both strings.
🌐
Guru99
guru99.com › home › java tutorials › how to compare two strings in java
How to compare two Strings in Java
November 8, 2024 - The first result returns a value of zero since the first and second strings are equal. The second result returns a value of thirty-two since the characters are different. Note: To ignore the cases, you can use the “compareToIgnoreCase” method. ... The Java StringUtils equals() method compares ...
🌐
LinkedIn
linkedin.com › pulse › string-comparison-java-methods-performance-saeed-anabtawi-
String Comparison in Java: Methods and Performance Implications
June 15, 2023 - If the strings are equal, it returns 0. If the string is lexicographically less than the argument string, it returns a value less than 0, and if it is lexicographically greater, it returns a value greater than 0. java String s1 = "abc"; String ...
🌐
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 - It returns a positive value (>0) if the first string is greater than the second one, a negative value if the first string is less than the second one, and 0 if both of them are equal.
🌐
Tpoint Tech
tpointtech.com › string-comparison-in-java
String Comparison in Java - Tpoint Tech
March 17, 2025 - The following example demonstrates string comparison using the equal to (==) operator. ... The String class compareTo() method compares values lexicographically and returns an integer value that describes if first string is less than, equal to or greater than second string. Suppose s1 and s2 ...
🌐
Medium
medium.com › @timothyjosephcw › how-to-compare-strings-in-java-3c7d84387b80
How to Compare Strings in Java. In Java, comparing strings can be done… | by timothy joseph | Medium
October 21, 2024 - It returns: `0` if the strings are equal · A negative number if the first string is lexicographically less than the second string · A positive number if the first string is lexicographically greater than the second string · ```java String ...