Thinking about using our OCA 8 book to study for the Java Foundations Junior Associate exam? It covers most of the topics. See what other topics you need to learn and where to read about that. One of those topics is comparing two Strings using compareTo(). Luckily there are only two things you need to know.
What does compareTo() return.
Scenario | Result |
---|---|
The two Strings are equal | 0 |
The first String sorts earlier alphabetically | a negative number |
The first String sorts later alphabetically | a positive number |
Let’s look at some examples:
String first = "abc"; String second = "def"; System.out.println(first.equals(first)); // true System.out.println(first.compareTo(first)); // 0 System.out.println(first.equals(second)); // false System.out.println(first.compareTo(second)); // -3 System.out.println(second.compareTo(first)); // 3
Don’t worry. You don’t have to know the answer is -3. You just have to know that it is negative or positive or zero.
How is alphabetically defined?
For the exam, you need to know that numbers sort before letters and uppercase sorts before lowercase. What do you think the output of this code is?
public class PlayTest { public static void main(String[] args) { String nums = "123"; String uppercase = "ABC"; String lowercase = "abc"; printComparison(nums, nums); printComparison(nums, uppercase); printComparison(nums, lowercase); System.out.println(); printComparison(uppercase, nums); printComparison(uppercase, uppercase); printComparison(uppercase, lowercase); System.out.println(); printComparison(lowercase, nums); printComparison(lowercase, uppercase); printComparison(lowercase, lowercase); } private static void printComparison(String one, String two) { int result = one.compareTo(two); if (result == 0) { System.out.println("0"); } else if (result < 0) { System.out.println("negative"); } else { System.out.println("positive"); } }
The answer is:
0
negative
negative
positive
0
negative
positive
positive
0
Make sure you can fill this in by yourself. You should also know the space sorts before letters. For example, printComparison(“ABC”, “A C”); prints out a positive number.
Summary
There’s not much you have to memorize. The key facts are:
- The method name is compareTo()
- Numbers sort before capital letters which sort before lowercase letters
- Spaces sort before letters
Practice Questions
Question 1
What does the following output? (Choose all that apply)
String first = "MOO"; String second = "moo"; System.out.println(first.compareTo(second)); System.out.println(first.equals(second));
A: A negative number
B: Zero
C: A positive number
D: true
E: false
F: none of the above
Question 2
What does the following output? (Choose all that apply)
String first = "moo"; String second = " moo"; System.out.println(first.compareTo(second)); System.out.println(first.equals(second));
A: A negative number
B: Zero
C: A positive number
D: true
E: false
F: none of the above
Question 3
What does the following output? (Choose all that apply)
String first = "MOO"; String second = "MOO"; System.out.println(first.compareTo(second)); System.out.println(first.equals(second));
A: A negative number
B: Zero
C: A positive number
D: true
E: false
F: none of the above
Question 4
What does the following output? (Choose all that apply)
String first = "moo"; String second = "MOO"; System.out.println(first.compare(second)); System.out.println(first.equals(second));
A: A negative number
B: Zero
C: A positive number
D: true
E: false
F: none of the above
Question 5
What does the following output? (Choose all that apply)
String first = "MOO"; String second = "1"; System.out.println(first.compareTo(second)); System.out.println(first.equals(second));
A: A negative number
B: Zero
C: A positive number
D: true
E: false
F: none of the above