🌐
Codekru
codekru.com › home › java string compareto() method
Java String compareTo() method - Codekru
June 12, 2022 - Method declaration – public int compareTo(String anotherString).
🌐
Oreate AI
oreateai.com › blog › understanding-the-compareto-method-in-java-a-deep-dive › 35cf53016b47c997048f5f3caa0a99e0
Understanding the compareTo Method in Java: A Deep Dive - Oreate AI Blog
December 30, 2025 - It’s important to note that when implementing your own classes that need comparison functionality—say you've created a class called Person—you'd implement Comparable and override compareTo(Person otherPerson).
Discussions

comparable - How to implement compareTo method in Java and what does it mean - Stack Overflow
I know that the Java Comparable interface has only one method, which has an int return type. However, I'm not sure what I am actually doing when I want to override the compareTo method. It's important to me to understand programming and not just memorizing it. For example, in the following ... More on stackoverflow.com
🌐 stackoverflow.com
Understanding the compareTo() method
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. The result is positive if the first string is lexicographically greater than the second string else the result would be negative. More on reddit.com
🌐 r/javahelp
16
15
August 8, 2020
Can somebody help me with java (compareto()???)
A few thoughts: compareTo does not return -1, 0, 1. compareTo only guarantees that it returns either a negative, zero, or positive number. It would be incorrect to write code that compares the output of a call to compareTo against -1 or 1. e.g. if (a.compareTo(b) == -1) // wrong if (a.compareTo(b) < 0) // right CompareTo is used to establish the idea that one object can be greater than, equal to, or less than another object. For instance, Integers are Comparable, and they have a compareTo method. One Integer is larger than another if the int value it contains is larger (numerically) than the other. Strings are Comparable. Doubles are Comparable. As you noted, many other things can be Comparable, such as Students. Java chose to compare Strings lexicographically. This is similar to how a real-life dictionary would sort words in the English alphabet. Essentially, Strings are sorted the same way they would be sorted in the dictionary, and we use the ASCII table as the alphabet (instead of a regular 26-letter alphabet). This implies that capital letters come before lower-case letters. (e.g. "Z" < "a") The ternary operator is ? :. The syntax is: b ? x : y Where b is a boolean expression. x and y are expressions. If b is true, then the ternary operator returns x. Otherwise, it returns y. For instance, rather than writing: int x = 3; // or whatever other value you want if (x < 5) System.out.println("x is less than 5"); else System.out.println("x is greater than or equal to 5"); You can instead write: int x = 3; // or whatever other value you want System.out.println(x < 5 ? "x is less than 5" : "x is greater than or equal to 5"); More on reddit.com
🌐 r/learnprogramming
5
6
January 20, 2013
[Java] Need help with a Generic compareTo
public class Sorter> { ... public int compareTo(T other) { if(this.compareTo(other) > 0) return 1; else if(this.compareTo(other) == 0) return 0; else return -1; } } I'm not sure I understand what you're trying to do with your compareTo() method. You're attempting to compare a Sorter object with a T object? That is not a logical comparison. I'm not sure why you even need this method. In your sortIds(List, List) method, you're correctly using the compareTo() method of the Comparable objects. === Also in lines 17-19, do I need to have the cast there, or is there another way around it? The casts that you're performing in your constructor are not safe. There is no way of knowing that the object at temp[0] is actually of type T. For example, if you created a new Sorter(scanner), the code in your constructor would obviously not be correct, since temp is an array of Strings, and you'd be casting them to Integer. It the constructor may run without error, due to the type-erasured nature of generics, but you'll eventually run into problems when you think you have a List, but it is actually List, for example. I don't even think that code should be part of the constructor. You should read the input in a separate method, (such as your main() method), where you can convert the input to the appropriate Comparable types (e.g. parsing an integer from the String input), and then pass your type-safe IDs to your Sorter class. More on reddit.com
🌐 r/learnprogramming
2
1
January 21, 2013
🌐
Tutorialspoint
tutorialspoint.com › home › java › java string compareto method
Java String compareTo Method
September 1, 2008 - public class Test { public static ... are not immutable"); int result = str1.compareTo( str2 ); System.out.println(result); result = str2.compareTo( str3 ); System.out.println(result); } }...
🌐
DEV Community
dev.to › tpointtechadu › mastering-compareto-in-java-a-complete-guide-with-examples-c83
Mastering compareTo() in Java: A Complete Guide with Examples - DEV Community
May 13, 2025 - For example, when you want to sort a list of student names alphabetically or rank players by their scores, using compareTo() helps streamline the logic in a consistent and reliable way.
🌐
PREP INSTA
prepinsta.com › home › java tutorial › java string compareto() method
Java String compareTo() Method
June 7, 2023 - public class Main { public static void main(String[] args) { String myStr1 = "PrepInsta Prime"; String myStr2 = "PrepInsta"; System.out.println(myStr1.compareTo(myStr2)); // Returns Positive value } } ... Note: Here the output comes is 7 because the first string is lexicographically longer than the second string.. Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription ... Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:
🌐
Oodlescoop
oodlescoop.com › programs › java › java-program-example-to-demonstrate-string-compareto-method
Programs Java : Java Program example to demonstrate String compareTo method
May 31, 2020 - public class StringCompareTo { public static void main(String[] args) { String str1 = "Welcome to oodlescoop"; String str2 = "Welcome to oodlescoop"; String str3 = "Hello all"; String str4 = "Welcome to oodlescoop tutorials"; String str5 = "WELCOME TO OODLESCOOP"; System.out.println(str1.compareTo(str2) + "\t: strings are equal"); System.out.println(str1.compareTo(str3) + "\t: first string is greater"); System.out.println(str1.compareTo(str4) + "\t: second string is greater"); System.out.println(str1.compareTo(str5) + "\t: unequal due to case of the text"); } } This Java program demonstrates the use of the compareTo method in the String class to compare two strings lexicographically.
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() 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
🌐
Scribd
scribd.com › document › 414417359 › Document-CompareTo
Java String compareTo() Method Explained | PDF | Computer Science | Computer Data
The document discusses the Java String compareTo() method, which compares two strings lexicographically and returns an integer indicating whether the first string is less than, equal to, or greater than the second string. It provides examples of using compareTo() to compare strings, and notes that compareTo() is case sensitive, while compareToIgnoreCase() performs a case-insensitive comparison.Read more ·
🌐
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.
🌐
LeetCode
leetcode.com › problems › backspace-string-compare
Backspace String Compare - LeetCode
Can you solve this real interview question? Backspace String Compare - Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.
🌐
DEV Community
dev.to › tpointtechadu › understanding-compareto-in-java-a-beginners-guide-1nno
Understanding compareTo() in Java: A Beginner's Guide - DEV Community
May 8, 2025 - The output of the compareTo Java method is always an integer. There are three general outcomes: A zero (0) means the two values being compared are equal. A positive number means the first value is greater than the second. A negative number means the first value is less than the second. Let’s put this in perspective with a basic example...
🌐
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
It's important to me to understand programming and not just memorizing it. For example, in the following code we have an int variable spaceshipClassComparison, which is equal to this.spaceshipClass.compareTo(other.spaceshipClass).
🌐
Quora
quora.com › What-does-the-compareto-function-do-in-Java
What does the compareto() function do in Java? - Quora
Answer (1 of 3): The method compareTo() is used for comparing two strings. Each character of both the strings is converted into a Unicode value for comparison. It returns a positive number, negative number or 0 (difference of character value).
🌐
W3Schools
w3schools.com › java › java_operators_comparison.asp
Java Comparison Operators
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
🌐
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

🌐
Medium
medium.com › @neerajrs124 › understanding-the-compareto-method-in-java-a-beginners-guide-bfae752540cf
Understanding the CompareTo Method in Java: A Beginner’s Guide | by Tpoint Tech | Medium
May 6, 2025 - Consider an example where we compare two strings: “apple” and “banana.” Since the first character of “apple” (‘a’) is less than the first character of “banana” (‘b’), the compareTo method will return a negative number, ...
🌐
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); }
🌐
Programiz
programiz.com › java-programming › library › string › compareto
Java String compareTo()
The compareTo() method takes a single parameter. ... class Main { public static void main(String[] args) { String str1 = "Learn Java"; String str2 = "Learn Java"; String str3 = "Learn Kolin"; int result; // comparing str1 with str2