🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The method returns 0 if the string is equal to the other string.
🌐
W3Schools
w3schools.in › java › examples › compare-two-strings
Java Program to Compare Two Strings - W3Schools
So to perform a comparison which ignores case differences, you have to use equalsIgnoreCase() method. As it compares two strings, it considers A-Z to be the same as a-z. In this Java program, it ignores the upper and lower case issue and compares both the strings.
🌐
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 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. By compareTo() method. == operator compares the references of the string objects not the actual content of the string objects. It returns true if references of the compared strings are equal, otherwise ...
🌐
W3Schools
w3schools.com › java › ref_string_equals.asp
Java String equals() Method
String myStr1 = "Hello"; String ... true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically....
🌐
Cach3
w3schools.com.cach3.com › java › ref_string_compareto.asp.html
Java String compareTo() Method - W3Schools
The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-compareto-method-with-examples
Java String compareTo() Method with Examples - GeeksforGeeks
January 20, 2025 - The Java compareTo() method compares the given string with the current string lexicographically. It returns a positive number, a negative number, or 0.
🌐
W3Schools
w3schools.com › java › java_ref_string.asp
Java String Reference
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods · charAt() codePointAt() codePointBefore() codePointCount() compareTo() compareToIgnoreCase() concat() contains() contentEquals() copyValueOf() endsWith() equals() equalsIgnoreCase() format() getBytes() getChars() hashCode() indexOf() isEmpty() join() lastIndexOf() length() matches() offsetByCodePoints() regionMatches() replace() replaceAll() replaceFirst() split() startsWith() subSequence() substring() toCharArray() toLowerCase() toString() toUpperCase() trim() valueOf() Java Math Methods ·
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
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 66478602 › compareto-java-method-on-strings-return-value
comparable - compareTo java method on Strings return value - Stack Overflow
Person person1 = new Person("Joe", "Rock"); Person person2 = new Person("Joe", "Stone"); person1.compareTo(person2) ... Note: In case the Strings in the object are changing and not equal, the negative number will be different to what it is now. ... it isn't always negative, it depends on which of the values of greater. Here's a quick read on the topic: w3schools.com/java/….
🌐
W3Schools
w3schools.com › java › ref_string_comparetoignorecase.asp
Java String compareToIgnoreCase() 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"; System.out.println(myStr1.compareToIgnoreCase(myStr2));
🌐
Programiz
programiz.com › java-programming › library › string › compareto
Java String compareTo()
The compareTo() method takes the letter case (uppercase and lowercase) into consideration. class Main { public static void main(String[] args) { String str1 = "Learn Java"; String str2 = "learn Java"; int result; // comparing str1 with str2
🌐
W3Schools
w3schools.invisionzone.com › server scripting › java/jsp/j2ee
comparing length of Strings - Java/JSP/J2EE - W3Schools Forum
June 14, 2012 - Hello, I am trying to comprehend how to compare the length between Strings.i have the following, to compare the length of 10 countries inserted by the user. It also orders them from lowest to highest: public void load() { teclado=new Scanner(System.in); paises=new String[10]; for(int f=0;f
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-compareto-method-example
Java String compareTo() Method with examples
This method takes string as an argument and returns an integer. It returns positive number, negative number or zero based on the comparison: str1.compareTo(str2) returns positive number, if str1 > str2
🌐
Sentry
sentry.io › sentry answers › java › how to compare strings in java
How to compare strings in Java | Sentry
public class Main { public static ... } } ... If you want to find out if a String is “bigger” or “smaller” than another string (that is, whether it comes before or after alphabetically), use String.compareTo()....
🌐
W3Schools
w3schools.com › java › java_operators_comparison.asp
Java Comparison Operators
Operators Arithmetic Assignment Comparison Logical Precedence Code Challenge Java Strings
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-strings-in-java
Compare two Strings in Java - GeeksforGeeks
July 11, 2025 - Other Methods to Compare Strings ... Define a function to compare values with the following conditions : if (string1 > string2) it returns a positive value....
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - String string1 = "using equals ... The compareTo() method returns an int type value and compares two Strings character by character lexicographically based on a dictionary or natural ordering....
🌐
TechVidvan
techvidvan.com › tutorials › java-string-compareto-method
Java String compareTo() Method with Examples - TechVidvan
March 7, 2024 - The compareTo() function of the Java String class lexicographically compares the inputted string with the currently displayed string.