Java .compareTo()
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.
Syntax
string1.compareTo(string2);
Parameters:
string1
: The string on which.compareTo()
is called.string2
: The string to compare withstring1
.
Return value:
- Returns
0
if both strings are equal. - Returns a positive number if
string1
is lexicographically greater thanstring2
. - Returns a negative number if
string1
is lexicographically less thanstring2
.
A way to think about this lexicographical evaluation is noting the Unicode values for these character sets:
Character Set | Range | Example |
---|---|---|
1 - 9 |
49 - 57 | "7".compareTo("3"); -> 55 - 51 = 4 |
A - Z |
65 - 90 | "A".compareTo("B"); -> 65 - 66 = -1 |
a - z |
97 - 122 | "z".compareTo("w"); -> 122 - 119 = 3 |
Example 1: Comparing Equal Strings
This example uses .compareTo()
to compare "Codecademy"
to "Codecademy"
:
class CompareStringsLexicographically {public static void main(String[] args) {String word1 = "Codecademy";String word2 = "Codecademy";System.out.println(word1.compareTo(word2));}}
Here is the output:
0
Example 2: First String is Lexicographically Less
This example uses .compareTo()
to compare "Codecademy"
to "codecademy"
:
class CompareStringsLexicographically {public static void main(String[] args) {String word1 = "Codecademy";String word2 = "codecademy";System.out.println(word1.compareTo(word2));}}
Here is the output:
-32
Example 3: First String is Lexicographically Greater
This example uses .compareTo()
to compare "codecademy"
to "Codecademy"
:
class CompareLexicographically {public static void main(String[] args) {String word1 = "codecademy";String word2 = "Codecademy";System.out.println(word1.compareTo(word2));}}
Here is the output:
32
Frequently Asked Questions
1. Is .compareTo()
case-sensitive?
Yes, it is. Uppercase letters have lower Unicode values than lowercase letters. For example, "Apple".compareTo("apple")
returns a negative number.
2. How does .compareToIgnoreCase()
differ?
The .compareToIgnoreCase()
method compares two strings lexicographically but ignores case differences. For example:
"Java".compareToIgnoreCase("java") // Output: 0
3. Can .compareTo()
be used for sorting strings?
Yes. You can use .compareTo()
in sorting algorithms or with data structures like TreeSet
or Collections.sort()
to sort strings alphabetically.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours