Menu
Coddy logo textTech

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). Returns true if the strings have the same characters in the same order, false otherwise. This method is case-sensitive.
  • equalsIgnoreCase(String other): Similar to equals, but ignores case. Returns true if the strings have the same characters, regardless of their case, false otherwise.
  • 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 to compareTo, 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); // 0
challenge icon

Challenge

Easy

Create a method named compareStrings that takes two strings as input and performs the following comparisons:

  1. Checks if the two strings are equal using equals and prints the result.
  2. Checks if the two strings are equal (ignoring case) using equalsIgnoreCase and prints the result.
  3. Compares the two strings using compareTo and prints the result.
  4. Compares the two strings (ignoring case) using compareToIgnoreCase and 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: 0

Cheat sheet

In Java, use specific methods to compare strings, not the == operator which compares memory addresses:

  • equals(Object other): Case-sensitive equality comparison
  • equalsIgnoreCase(String other): Case-insensitive equality comparison
  • compareTo(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); // 0

Try 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);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals