String::length is a method reference. Using it is equivalent to

Comparator.comparing(s -> s.length())

So it compares strings by comparing their length.

Answer from JB Nizet on Stack Overflow
🌐
Java67
java67.com › 2016 › 10 › how-to-compare-string-by-their-length-in-java8.html
How to Compare and Sort String by their length in Java? Example | Java67
You can define such a one-off Comparator by using Anonymous inner class as shown in this article and further use it to sort a list of String on their length. This is a very useful technique and works fine in Java 6 and 7 but works fantastically well in Java 8 due to less clutter provided by the brand new feature called lambda expressions.
🌐
SheCodes
shecodes.io › athena › 6673-comparing-string-length-in-java
[Java] - Comparing String Length in Java - SheCodes Athena | SheCodes
Learn how to check if one string is longer than another with Java with the help of code 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.
🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
Tip: Use the equals() method to compare two strings without consideration of Unicode values. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to ...
🌐
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)
The String class has a number of methods for comparing strings and portions of strings. The following table lists these methods. The following program, RegionMatchesDemo, uses the regionMatches method to search for a string within another string: public class RegionMatchesDemo { public static void main(String[] args) { String searchMe = "Green Eggs and Ham"; String findMe = "Eggs"; int searchMeLength = searchMe.length(); int findMeLength = findMe.length(); boolean foundIt = false; for (int i = 0; i <= (searchMeLength - findMeLength); i++) { if (searchMe.regionMatches(i, findMe, 0, findMeLength)) { foundIt = true; System.out.println(searchMe.substring(i, i + findMeLength)); break; } } if (!foundIt) System.out.println("No match found."); } } The output from this program is Eggs.
Find elsewhere
🌐
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.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-compareto-method-example
Java String compareTo() Method with examples
September 16, 2022 - In the above code snippet, the second compareTo() statement returned the length in negative number, this is because we have compared the empty string with str1 while in first compareTo() statement, we have compared str1 with empty string. ... public class JavaExample { public static void ...
🌐
Javatpoint
javatpoint.com › java-string-length
Java String length() Method - javatpoint
Java String length() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string length in java etc.
Top answer
1 of 16
6153

== 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
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-length-method-with-examples
Java String length() Method - GeeksforGeeks
December 23, 2024 - Return Type: The return type of the length() method is int. ... public class Geeks { public static void main(String[] args){ // Here str is a string object String str = "GeeksforGeeks"; System.out.println(str.length()); } } ... // whether the ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-strings-in-java
Compare two Strings in Java - GeeksforGeeks
July 11, 2025 - It compares and returns the values as follows: if (string1 > string2) it returns a positive value. if both the strings are equal lexicographically i.e.(string1 == string2) it returns 0. if (string1 < string2) it returns a negative value.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Find-Java-String-Length-Example-Tutorial
How do I find the Java String length?
String javaString = " String length ... of a String when the trim method is called first. Be careful not to confuse the String length() method with the length property of an array....
🌐
Tutorialspoint
tutorialspoint.com › java › java_string_length.htm
Java - String length() Method
This method returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string. Here is the syntax of this method − Here is the detail of parameters − This will produce the following result −
🌐
Sentry
sentry.io › sentry answers › java › how to compare strings in java
How to compare strings in Java | Sentry
public class Main { public static ... than another string (that is, whether it comes before or after alphabetically), use String.compareTo()....
🌐
Opensource.com
opensource.com › article › 19 › 9 › compare-strings-java
How to compare strings in Java | Opensource.com
September 20, 2019 - Two strings are considered equal ignoring case if they * are of the same length and corresponding characters in the two strings * are equal ignoring case. * * <p> Two characters {@code c1} and {@code c2} are considered the same * ignoring case if at least one of the following is true: * <ul> * <li> The two characters are the same (as compared by the * {@code ==} operator) * <li> Applying the method {@link * java.lang.Character#toUpperCase(char)} to each character * produces the same result * <li> Applying the method {@link * java.lang.Character#toLowerCase(char)} to each character * produces t