Comparing Two String Objects Using compareTo Methods
String Methods
We've compiled some helpful String methods below.
![]() |
Methods that are part of the AP Computer Science A Java subset are denoted by the APCSA label. |
A complete list of String methods can be found in the String API.
int compareTo(String anotherString)
This method returns
- 0 if this
Stringhas the same value asanotherString. - a value less than 0 if this
Stringis lexicographically less thananotherString. Lexicographically is also referred to as dictionary order. It is the way we alphabetize words. In computers each character is assigned a number based on the ASCII codes. In this system, uppercase letters have a lower value than uppercase letters. In other words, uppercase letters come before lowercase letters in alphabetical order. For example, "apple" is lexicographically before "axel", since "p" comes before "x". - a value greater than 0 if this
Stringis lexicographically greater thananotherString.
It is helpful to think of lexicographical order a little like alphabetical order, but uppercase letters come before lowercase letters. Look at the two strings that are being compared, draw a < above the compareTo method if the first word comes before the second word and a > if the first word comes after the second. If it is less than, the return value is negative. If it is greater than, the return value is positive. This visualization helps to illustrate this process.
Your Turn
Let's try it in the Java Playground.
- Add a
Stringwith the same value asnameand compare the two.
valueis assigned a value greater than 0, sinceDcomes afterA.value2is assigned a value less than 0, sinceAcomes beforeD.
int compareToIgnoreCase(String anotherString)
This method is similar to the compareTo method, but doesn't distinguish between uppercase and lowercase letters.
This method returns
- 0 if this
Stringhas the same value asanotherString. - a value less than 0 if this
Stringis lexicographically less thananotherString, while ignoring the difference between upper and lowercase letters. - a value greater than 0 if this
Stringis lexicographically greater thananotherString, while ignoring the difference between upper and lowercase letters.
Your Turn
Let's try it in the Java Playground.
- Predict the value of
value. - Run the code to determine whether your prediction is correct.
valueis assigned0.
boolean endsWith(String suffix)
This method returns
trueif thisStringends withsuffixandfalseotherwise.
Your Turn
Let's try it in the Java Playground.
- Predict the values of
valueandvalue2. - Run the code to determine whether your predictions are correct.
valueis assignedtruesinceAwesomenessends withness.value2is assignedfalse. WhileAwesomenesscontainssome, it doesn't end withsome.
Complete List of String Learn Tutorials
- Learn: Declaring Strings
- Learn: Constructing a String
- Learn: String Indexing
- Learn: Comparing Strings with compareTo methods
- Learn: Comparing Strings with equals methods
- Learn: Creating Substrings
- Learn: Creaing a new String with the Case Changed
Resources
- Learn: Declaring Strings
- Learn: Introduction to Arithmetic Expressions
- Learn: Introduction to Console Input and Output
- Learn: Formatting Output with Escape Sequences and Text Blocks
- Practice: Evaluating Expressions that Use String Methods
- Practice: Write Code that Uses the String Methods
- Practice: AP Computer Science A String Free Response Questions