String Comparison
Part of the Fundamentals section of Coddy's Java journey — lesson 70 of 73.
In Java, comparing strings is not as straightforward as comparing primitive data types like integers or characters. You can't use the == operator to compare strings for equality because strings are objects, and == would compare their memory addresses, not their contents. Instead, you need to use specific methods provided by the String class to compare the actual characters within the strings.
Here are the primary methods for string comparison:
equals(Object other): Compares the string to another object (usually another string). Returnstrueif the strings have the same characters in the same order,falseotherwise. This method is case-sensitive.
equalsIgnoreCase(String other): Similar toequals, but ignores case. Returnstrueif the strings have the same characters, regardless of their case,falseotherwise.
compareTo(String other): Compares two strings lexicographically (based on their Unicode values). Returns 0 if the strings are equal, a negative number if the first string is less than the second, and a positive number if the first string is greater than the second. This method is case-sensitive.
compareToIgnoreCase(String other): Similar tocompareTo, but ignores case.
Here's an example:
String str1 = "hello";
String str2 = "hello";
String str3 = "HELLO";
String str4 = "world";
boolean eq1 = str1.equals(str2); // true
boolean eq2 = str1.equals(str3); // false
boolean eq3 = str1.equalsIgnoreCase(str3); // true
int cmp1 = str1.compareTo(str2); // 0
int cmp2 = str1.compareTo(str4); // negative value
int cmp3 = str4.compareTo(str1); // positive value
int cmp4 = str1.compareToIgnoreCase(str3); // 0Challenge
EasyCreate a method named compareStrings that takes two strings as input and performs the following comparisons:
- Checks if the two strings are equal using
equalsand prints the result. - Checks if the two strings are equal (ignoring case) using
equalsIgnoreCaseand prints the result. - Compares the two strings using
compareToand prints the result. - Compares the two strings (ignoring case) using
compareToIgnoreCaseand prints the result.
Example output for the input strings HELLO and hello:
HELLO equals hello: false
HELLO equalsIgnoreCase hello: true
HELLO compareTo hello: -32
HELLO compareToIgnoreCase hello: 0Cheat sheet
In Java, use specific methods to compare strings, not the == operator which compares memory addresses:
equals(Object other): Case-sensitive equality comparisonequalsIgnoreCase(String other): Case-insensitive equality comparisoncompareTo(String other): Case-sensitive lexicographic comparison (returns 0 if equal, negative if first < second, positive if first > second)compareToIgnoreCase(String other): Case-insensitive lexicographic comparison
String str1 = "hello";
String str2 = "HELLO";
boolean eq1 = str1.equals(str2); // false
boolean eq2 = str1.equalsIgnoreCase(str2); // true
int cmp1 = str1.compareTo(str2); // positive value
int cmp2 = str1.compareToIgnoreCase(str2); // 0Try it yourself
import java.util.Scanner;
public class Main {
public static void compareStrings(String str1, String str2) {
// Write your code here
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str1 = scanner.nextLine();
String str2 = scanner.nextLine();
compareStrings(str1, str2);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else