🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
The compareTo() method compares two strings lexicographically.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 8 )
2 days ago - This interface is a member of the Java Collections Framework. Since: 1.2 · See Also: Comparator · int compareTo(T o) Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the ...
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › java › number_compareto.htm
Java - compareTo() Method
Java Vs. C++ ... The method compares the Number object that invoked the method to the argument. It is possible to compare Byte, Long, Integer, etc. However, two different types cannot be compared, both the argument and the Number object invoking ...
🌐
Baeldung
baeldung.com › home › java › core java › guide to implementing the compareto method
Guide to Implementing the compareTo Method | Baeldung
May 29, 2025 - TreeMap and TreeSet are two implementations from the Java Collections Framework that assist us with the automatic sorting of their elements. We may use objects that implement the Comparable interface in a sorted map or as elements in a sorted set. Let’s look at an example of a custom class that compares players based on the number of goals they have scored: @Override public int compareTo(FootballPlayer anotherPlayer) { return Integer.compare(this.goalsScored, anotherPlayer.goalsScored); }
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-equals-compareto-equalsignorecase-and-compare
Java | ==, equals(), compareTo(), equalsIgnoreCase() and compare() - GeeksforGeeks
January 22, 2026 - The compareTo() method compares strings lexicographically based on their Unicode values. It returns: 0 if both strings are equal · A negative value if the first string is smaller · A positive value if the first string is greater · JAVA ·
🌐
Zero To Mastery
zerotomastery.io › blog › java-compareto-method
Beginner's Guide To compareto In Java (With Code Examples) | Zero To Mastery
March 3, 2025 - It’s Java’s built-in method for comparing objects, helping you sort lists, rank values, and organize data the way you want. But if you’ve never used it before, it might seem a little weird. Why does it return -1, 0, or 1 instead of true or false? Don’t worry because we’ll cover all that and more, so that by the end of this guide, you’ll not only understand how compareTo works - you’ll be confident using it to compare strings, numbers, and even custom objects.
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparable-interface-in-java-with-examples
Java Comparable Interface - GeeksforGeeks
It enables objects to be compared and sorted automatically without using an external Comparator. It contains the compareTo() method, which compares the current object with another object.
Published   2 weeks ago
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-compareto-method-example
Java String compareTo() Method with examples
September 16, 2022 - 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.
🌐
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

🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_compareto.htm
Java - String compareTo() Method
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.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string compareto() method
Java String CompareTo() Method
December 25, 2024 - The java string class compareTo() method returns the 0 value if both strings are lexicographically equal. If the compared string is greater lexicographically then the positive value is returned otherwise the negative value is returned.
🌐
Programiz
programiz.com › java-programming › library › string › compareto
Java String compareTo()
Become a certified Java programmer. Try Programiz PRO! ... The compareTo() method compares two strings lexicographically (in the dictionary order).
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 7 )
This interface is a member of the Java Collections Framework. Since: 1.2 · See Also: Comparator · int compareTo(T o) Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the ...
🌐
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.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java string compareto method with programming examples
Java String compareTo Method With Programming Examples
April 1, 2025 - In the above example, we have taken five input Strings and performed a basic comparison between them using the .compareTo() Java method. In the first comparison, we have ‘A’ greater than ‘G’ by 6 characters in the alphabet series, so it returns +6.
🌐
LabEx
labex.io › tutorials › java-how-to-use-compareto-method-to-compare-java-objects-414156
How to use compareTo() method to compare Java objects | LabEx
By the end, you'll have a solid ... part of the Java programming language. It is used to compare two objects of the same class and determine their relative order....
🌐
Stack Overflow
stackoverflow.com › questions › 72160928 › how-to-implement-compareto-method-in-java-and-what-does-it-mean
comparable - How to implement compareTo method in Java and what does it mean - Stack Overflow
This is where writing a custom (@Override) compareTo method comes in. By default the compareTo method returns either 1, 0 or -1. There is no inherent meaning to these numbers, in fact they can be any numbers you like (as long as they are different).
🌐
DEV Community
dev.to › tpointtechadu › a-beginners-guide-to-javas-compareto-method-c2p
A Beginner’s Guide to Java’s compareTo() Method - DEV Community
March 20, 2025 - Using compareTo for numbers is especially beneficial when sorting numerical lists or checking conditions in decision-making scenarios. Since numbers are naturally ordered, compareTo simplifies the process of determining which number is larger or smaller. Java allows developers to implement the Comparable interface in custom classes, enabling object-specific comparisons.
🌐
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.