The compareTo() method compares two strings lexicographically.

The comparison is based on the Unicode value of each character in the strings.

You need to call length() method for each string to compare lengths

if(one.length() > two.length()) ...
Answer from esmin on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 34728988 › how-to-compare-string-in-java-using-scanner-object
How to compare string in java using scanner object - Stack Overflow
You are comparing the String so what is your question? Is this a good way? no, use a Map instead, But will it work? sure. ... next() will read up to the next space, so mystring1.equals("south korean") will always return false. ... In that case, I suggest trying to make your question clearer. This forum is not a general code review site. You need to ask a specific question to get an answer. ... Scanner input = new Scanner(System.in); String myString1 = new String ("state"); String myString2 = new String ("capitalstate"); System.out.println ("input state"); myString1=input.next(); if (myString1.contains("malaysia")) { myString2 = "kuala lumpur"; System.out.println ("capital state for " +myString1+ " is " +myString2); } else if (myString1.equals("south korean")) { myString2 = "seoul"; System.out.println ("capital state for " +myString1+ " is " +myString2); }
Discussions

in java, use String::length for Comparator.comparing() - Stack Overflow
String::length is a method reference. Using it is equivalent to ... So it compares strings by comparing their length. ... Java 8 introduces lambda syntax to the Java language, this is a small part of that, it allows you to pass a method as a lambda. More on stackoverflow.com
🌐 stackoverflow.com
Java : scanner input comparison with string - Java - Code with Mosh Forum
` My java code is : canner sc = new Scanner(System.in) ; String y = "" ; String z = "a" ; System.out.print("Type a number : "); y = sc.next(); System.out.println( y + " == " +z+ " :"); System.out.println( y == z); The result printout : Type a number : a a == a : false However, my expectation is TRUE More on forum.codewithmosh.com
🌐 forum.codewithmosh.com
0
August 24, 2021
java - compareTo and Strings using a Scanner - Stack Overflow
I am implementing a form of leftist min heap, which stores arbitrary words by length. So, I have written a wrapper class for Scanner, and changed the compareTo, like so public class ScannerWrapper More on stackoverflow.com
🌐 stackoverflow.com
April 5, 2012
Comparing multiple string lengths in array Java - Stack Overflow
This is what's asked of us: Ask the English version of the first names of three of your family members using showInputDialog() method of the JOptionPane class. Display the name of the family membe... More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
🌐
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.
🌐
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
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; /* * Java Program to sort list of String by their length in JDK 8 */ public class SortingListOfStringByLength{ public static void main(String[] args) { // In Java 8 System.out.println("Sorting List of String by length in Java 8 ======"); List<String> cities = new ArrayList<>(Arrays.asList("London", "Tokyo", "NewYork")); System.out.println("The original list without sorting"); System.out.println(cities); cities.sort((first, second) -> Integer.compare(first.length(), second.length())); System
🌐
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)
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."); } }
🌐
Code with Mosh
forum.codewithmosh.com › java
Java : scanner input comparison with string - Java - Code with Mosh Forum
August 24, 2021 - ` My java code is : canner sc = new Scanner(System.in) ; String y = "" ; String z = "a" ; System.out.print("Type a number : "); y = sc.next(); System.out.println( y + " == " +z+ " :"); System.out.println( y == z); T…
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
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 characters) and ...
🌐
Learn Java
javatutoring.com › java-string-length
4 Methods To Find Java String Length() | Str Length - Learn Java
February 25, 2026 - String in Java has many predefined methods in it. T · By making use of this predefined method and giving our input string as the String whose length is to be found, we can directly display the length in our output screen.
🌐
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.
🌐
Stack Overflow
stackoverflow.com › questions › 10022635 › compareto-and-strings-using-a-scanner
java - compareTo and Strings using a Scanner - Stack Overflow
April 5, 2012 - So, I have written a wrapper class ... sc and a String, current public int compareTo(String str){ if(current.length() > str.length()) return -1; if(current.length() > str.length()) return 1; else return 0; }...
🌐
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?
The String length method includes the whitespace padding at the beginning and the end of the String. Quite often, when validating input or manipulating text, you want to eliminate leading and trailing whitespace. This can be achieved through the use of the Java String’s trim method.
🌐
Rosetta Code
rosettacode.org › wiki › Compare_length_of_two_strings
Compare length of two strings - Rosetta Code
March 11, 2026 - import java.util.ArrayList; import java.util.Comparator; import java.util.List; void printCompare(String stringA, String stringB) { if (stringA.length() > stringB.length()) { System.out.printf("%d %s%n", stringA.length(), stringA); System.out.printf("%d %s%n", stringB.length(), stringB); } else { System.out.printf("%d %s%n", stringB.length(), stringB); System.out.printf("%d %s%n", stringA.length(), stringA); } } void printDescending(String...
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
🌐
Stack Overflow
stackoverflow.com › questions › 16848532 › compare-to-scanner-string-inputs
java - Compare to Scanner String inputs - Stack Overflow
Use the equals() or equalsIgnoreCase() method like if(testOne.equals(testTwo)){...} (depending upon your requirements) instead of using == (it compares object references and not the string text).
🌐
W3Schools
w3schools.com › java › ref_string_length.asp
Java String length() Method
Operators Arithmetic Assignment Comparison Logical Precedence Code Challenge Java Strings
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › string comparison in java
String Comparison in Java: 7 Methods Explained
April 28, 2025 - The equals() method compares the actual content of strings (character by character), while the == operator compares object references (whether two variables point to the same object in memory).