String compareTo() method - Syntax and Examples
Learn how to use the Java `compareTo()` method for comparing strings lexicographically. This tutorial covers syntax, parameters, return values, and practical examples.
This reference explains String compareTo() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
Introduction
The compareTo() method in Java is a powerful tool for comparing strings. It doesn't just tell you if two strings are the same or different; it tells you *how* they differ based on their lexicographical order (essentially, dictionary order). Think of it like how words are arranged alphabetically - that's what compareTo() does for your strings.
Syntax
public int compareTo(String anotherString)
Parameters
| Parameter | Description |
|---|---|
anotherString |
The string to compare with the current string. |
Return Value
The compareTo() method returns an integer value, which indicates the relationship between the two strings:
- If the current string is lexicographically less than
anotherString, it returns a negative integer. - If the current string is lexicographically greater than
anotherString, it returns a positive integer. - If the two strings are equal, it returns 0.
Examples
Example 1: Basic Comparison
This example demonstrates a simple comparison between two strings.
String str1 = "apple";
String str2 = "banana";
int result = str1.compareTo(str2);
System.out.println(result); // Output: -1
-1
Explanation: Since "apple" comes before "banana" alphabetically, str1.compareTo(str2) returns a negative value (-1).
Example 2: Strings are Equal
This example shows what happens when the strings being compared are identical.
String str1 = "orange";
String str2 = "orange";
int result = str1.compareTo(str2);
System.out.println(result); // Output: 0
0
Explanation: Because both strings are exactly the same, compareTo() returns 0.
Example 3: Strings in Reverse Order
This example illustrates a comparison where the first string comes after the second alphabetically.
String str1 = "zebra";
String str2 = "apple";
int result = str1.compareTo(str2);
System.out.println(result); // Output: 1
1
Explanation: "zebra" comes after "apple", so the result is a positive integer (1).
Example 4: Case Sensitivity
This example highlights that compareTo() is case-sensitive.
String str1 = "Apple";
String str2 = "apple";
int result = str1.compareTo(str2);
System.out.println(result); // Output: 1
1
Explanation: Because "Apple" (uppercase 'A') has a higher Unicode value than "apple" (lowercase 'a'), compareTo() considers "Apple" greater and returns a positive value.
Example 5: Comparing Strings with Numbers
This example demonstrates comparing strings that contain numbers. The comparison is based on the string representation, not numerical values.
String str1 = "file10";
String str2 = "file2";
int result = str1.compareTo(str2);
System.out.println(result); // Output: 1
1
Explanation: The comparison is done lexicographically, treating the strings as text. "file10" comes after "file2" because '1' comes after '2' when compared as characters.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.