๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_compareto.asp
Java String compareTo() Method
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_operators_comparison.asp
Java Comparison Operators
HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate ... W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
Like the comparator, the compareTo() method returns a number which is: Negative if the comparable should go first in a list. Positive if the other object should go first in a list. Zero if the order does not matter. Many native Java classes implement the Comparable interface, such as String and Integer.
๐ŸŒ
Cach3
w3schools.com.cach3.com โ€บ java โ€บ ref_string_compareto.asp.html
Java String compareTo() Method - W3Schools
public int compareTo(String string2) public int compareTo(Object object) ... Tabs Dropdowns Accordions Side Navigation Top Navigation Modal Boxes Progress Bars Parallax Login Form HTML Includes Google Maps Range Sliders Tooltips Slideshow Filter List Sort List ยท HTML CSS JavaScript SQL Python ...
๐ŸŒ
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.
๐ŸŒ
Zero To Mastery
zerotomastery.io โ€บ blog โ€บ java-compareto-method
Beginner's Guide To compareto In Java (With Code Examples) | Zero To Mastery
It gets to 'e' in "Alice" and 'i' in "Alicia", and since 'e' comes before 'i', Java returns -1โ€” meaning "Alice" comes first. However, if the words were exactly the same ("Alice".compareTo("Alice")), compareTo would return 0, because thereโ€™s nothing to compare!
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_comparetoignorecase.asp
Java String compareToIgnoreCase() Method
Java Examples Java Videos Java ... Compare two strings, ignoring lower case and upper case differences: String myStr1 = "HELLO"; String myStr2 = "hello"; System.out.println(myStr1.compareToIgnoreCase(myStr2)); Try it Yourself ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java โ€บ java number compareto method
Java Number compareTo Method
September 1, 2008 - public class Test { public static void main(String args[]) { Integer x = 5; //Integer value is greater than the argument (5>3) so, output is 1 System.out.println(x.compareTo(3)); //Integer value is equal to the argument so, output is 0 ...
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-string-compareto
Java String compareTo()
Java String compareTo() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string compareto in java etc.
๐ŸŒ
Javapractices
javapractices.com โ€บ topic โ€บ TopicAction.do
Java Practices->Implementing compareTo
type-safe enumeration : use compareTo, like any Object ยท collection or array : Comparable does not seem to be intended for these kinds of fields. For example, List, Map and Set do not implement Comparable. As well, some collections have no definite order of iteration, so doing an element-by-element comparison cannot be meaningful in those cases.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ java-string-compareto-method-example
Java String compareTo() Method with examples
Here we have three Strings and we are comparing them with each other using compareTo() method. public class JavaExample { public static void main(String args[]) { String str1 = "String method tutorial"; String str2 = "compareTo method example"; String str3 = "String method tutorial"; int var1 = str1.compareTo(str2); System.out.println("str1 and str2 comparison: "+var1); int var2 = str1.compareTo(str3); System.out.println("str1 and str3 comparison: "+var2); int var3 = str2.compareTo("compareTo method example"); System.out.println("str2 and string argument comparison: "+var3); } }
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ guide to implementing the compareto method
Guide to Implementing the compareTo Method - Java
May 29, 2025 - 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); }
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arrays_compare.asp
Java Arrays compare() Method
HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference AngularJS Reference jQuery Reference ยท HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples
๐ŸŒ
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
In cases when we compare obvious things like numbers, we don't need a custom compareTo method. So for example, if we want to know if 2 is greater than 1 we just use the appropriate operator: 2 > 1
๐ŸŒ
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.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ java string compareto() method
Java String CompareTo() Method
December 25, 2024 - While working with the compareTo() method and the Comparable interface, there are several exceptions to consider: NullPointerException: This occurs if you attempt to compare an object with null. Always check for null values before comparison. ClassCastException: This occurs if objects being compared are not of the same type. For example, attempting to compare an Integer with a String would throw this exception. import java.util.ArrayList; import java.util.Collections; public class ExceptionExample { public static void main(String[] args) { try { ArrayList Object list = new ArrayList<>(); list.add("Apple"); list.add(42); Collections.sort((ArrayList
๐ŸŒ
TechVidvan
techvidvan.com โ€บ tutorials โ€บ java-string-compareto-method
Java String compareTo() Method with Examples - TechVidvan
March 7, 2024 - public class TechVidvanCompareTo{ public static void main(String args[]) { String a1 = "hello"; String a2 = "hello"; String a3 = "meklo"; String a4 = "hemlo"; System.out.println(a1.compareTo(a2)); System.out.println(a1.compareTo(a3)); System.out.println(a1.compareTo(a4)); } } ... Java allows ...
๐ŸŒ
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 to compare two objects of identical type and establish their relative sequence, 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 establishing the "natural order" for object comparison within a class.
๐ŸŒ
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