The general contract of Comparable.compareTo(o) is to return

  • a positive integer if this is greater than the other object.
  • a negative integer if this is lower than the other object.
  • 0 if this is equals to the other object.

In your example "ab2".compareTo("ac3") == -1 and "ab2".compareTo("ab3") == -1 only means that "ab2" is lower than both "ac3" and "ab3". You cannot conclude anything regarding "ac3" and "ab3" with only these examples.

This result is expected since b comes before c in the alphabet (so "ab2" < "ac3") and 2 comes before 3 (so "ab2" < "ab3"): Java sorts Strings lexicographically.

Answer from Tunaki on Stack Overflow
🌐
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.
🌐
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.
🌐
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

🌐
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
🌐
Codecademy
codecademy.com › docs › java › strings › .compareto()
Java | Strings | .compareTo() | Codecademy
July 14, 2025 - The .compareTo() method is a built-in Java method compares two strings lexicographically by evaluating the Unicode value of each character. This method is defined in the java.lang.String class and implements the Comparable<String> interface.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string compareto() method
Java String CompareTo() Method
December 25, 2024 - The compareTo() method compares strings based on the Unicode value of each character.
🌐
Scaler
scaler.com › home › topics › java string compareto() method
Java String compareTo() Method with Examples - Scaler Topics
May 10, 2023 - Thus, using the compareTo() in Java we can find the length of the string by comparing with an empty string as it returns the length of the non-empty string with positive and negative signs as per the position of the non-empty string.
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › compareTo
Java String compareTo() - Compare Strings Alphabetically | Vultr Docs
May 15, 2025 - The compareTo() method in Java is a crucial function from the String class that facilitates the alphabetical comparison of two strings. This method is commonly used in Java string comparison, especially for sorting strings, determining ...
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-compareto-method-example
Java String compareTo() Method with examples
The Java String compareTo() method is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java string compareto method with programming examples
Java String compareTo Method With Programming Examples
April 1, 2025 - Here is an example of compareTo() Java method. The comparison is based on the difference in the ASCII value of the characters. In general terms, a String is less than the other if it comes before the other in the dictionary.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java string compareto method
Java String compareTo Method
September 1, 2008 - The Java String compareTo() method is used to compare two strings lexicographically. It returns an integer value, and the comparison is based on the Unicode value of each character in the strings.
🌐
Zero To Mastery
zerotomastery.io › blog › java-compareto-method
Beginner's Guide To compareto In Java (With Code Examples) | Zero To Mastery
So the next time you sort a list of names, remember — Java is just running compareTo behind the scenes, checking letters one by one. Speaking of sorting, numbers work in a similar way. Let’s check that out next. Since compareTo sorts Strings alphabetically by checking each letter, you might wonder if it does the same thing for numbers?
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.compareto
String.CompareTo(String) Method (Java.Lang) | Microsoft Learn
In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value: <blockquote> ... </blockquote> If there is no index position at which they differ, then the shorter string ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-strings-in-java
Compare two Strings in Java - GeeksforGeeks
July 11, 2025 - Explanation: In the above example, we get the output as 6, because the compareTo() method compares strings lexicographically, finding the difference between the first mismatched characters 'J' (Unicode 74) and 'D' (Unicode 68), resulting in 74 - 68 = 6. The String.equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string. This method returns true if the argument is not null and the contents of both the Strings are same ignoring case, else false. ... // Java program to Compare two strings // lexicographically using String.equalsIgnoreCase() public class CompareStrings { public static void main(String args[]) { // Create two string objects with different cases String s1 = new String("Java"); String s2 = new String("JAVA"); System.out.println(s1.equalsIgnoreCase(s2)); } }
🌐
Javatpoint
javatpoint.com › java-string-compareto
Java String compareTo()
Java program to find the percentage of uppercase, lowercase, digits and special characters in a string
🌐
Guru99
guru99.com › home › java tutorials › java string manipulation with example
Java String compareTo() Method: How to Use with Examples
November 26, 2024 - Do I want to check if the String that was generated by some method is equal to something that I want to verify with? How do I compare two Strings? Use the method “compareTo” and specify the String that you would like to compare.
🌐
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.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › compareto in java
compareTo in Java: A Complete Guide with Practice Exercises
March 17, 2025 - In Java, the compareTo method serves ... returning a negative integer when the current object is lesser than the compared one, zero if they are equivalent, and a positive integer if the current object exceeds the compared object; effectively ...