Bird
Raised Fist0
Javaprogramming~5 mins

String comparison in Java - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the == operator check when used with strings in Java?
The == operator checks if two string variables point to the exact same object in memory, not if their text content is the same.
Click to reveal answer
beginner
How do you correctly compare the content of two strings in Java?
Use the equals() method, like string1.equals(string2), to check if two strings have the same characters in the same order.
Click to reveal answer
intermediate
What is the difference between equals() and equalsIgnoreCase() methods in Java strings?
equals() compares strings considering uppercase and lowercase letters, while equalsIgnoreCase() ignores case differences when comparing.
Click to reveal answer
intermediate
What does the compareTo() method return when comparing two strings?
It returns an integer: zero if strings are equal, a negative number if the first string is lexicographically less, and a positive number if it is greater.
Click to reveal answer
beginner
Why should you avoid using == for string content comparison in Java?
Because == checks if two references point to the same object, not if their text is the same. This can lead to wrong results when strings have the same content but are different objects.
Click to reveal answer
Which method correctly compares the text content of two strings in Java?
Astring1.equalsIgnoreCase(string2) only
Bstring1 == string2
Cstring1.compareTo(string2) == 1
Dstring1.equals(string2)
What does string1 == string2 check in Java?
AIf both strings have the same characters
BIf both strings are the same object in memory
CIf both strings have the same length
DIf both strings are lexicographically equal
What will "Hello".equalsIgnoreCase("hello") return?
Atrue
Bfalse
Cnull
DThrows an error
If string1.compareTo(string2) returns a negative number, what does it mean?
Astring1 is lexicographically less than string2
Bstring1 is lexicographically greater than string2
Cstring1 and string2 are equal
Dstring1 is null
Which is the best way to compare two strings ignoring case?
Astring1 == string2
Bstring1.equals(string2)
Cstring1.equalsIgnoreCase(string2)
Dstring1.compareTo(string2) == 0
Explain how to compare two strings in Java to check if they have the same text.
Think about object references vs content comparison.
You got /3 concepts.
    Describe what the compareTo() method does when comparing two strings.
    It's like dictionary order comparison.
    You got /4 concepts.

      Practice

      (1/5)
      1. Which method should you use in Java to check if two strings have exactly the same characters and case?
      String s1 = "Hello";
      String s2 = "hello";
      easy
      A. s1 == s2
      B. s1.compareTo(s2)
      C. s1.equalsIgnoreCase(s2)
      D. s1.equals(s2)

      Solution

      1. Step 1: Understand string equality in Java

        Using == compares references, not content, so it is not reliable for string content comparison.
      2. Step 2: Choose the method for exact match including case

        equals() checks if two strings have exactly the same characters and case, which is what is asked.
      3. Final Answer:

        s1.equals(s2) -> Option D
      4. Quick Check:

        Use equals() for exact string match [OK]
      Hint: Use equals() for exact string match including case [OK]
      Common Mistakes:
      • Using == instead of equals() for string content comparison
      • Using equalsIgnoreCase() when case matters
      • Using compareTo() for equality check
      2. Which of the following is the correct syntax to compare two strings s1 and s2 ignoring case in Java?
      easy
      A. s1.compareToIgnoreCase(s2)
      B. s1.equals(s2)
      C. s1.equalsIgnoreCase(s2)
      D. s1 == s2

      Solution

      1. Step 1: Identify method to compare strings ignoring case

        Java provides equalsIgnoreCase() to compare strings ignoring letter case differences.
      2. Step 2: Confirm syntax correctness

        s1.equalsIgnoreCase(s2) is the correct method call syntax.
      3. Final Answer:

        s1.equalsIgnoreCase(s2) -> Option C
      4. Quick Check:

        Use equalsIgnoreCase() to ignore case in string comparison [OK]
      Hint: Use equalsIgnoreCase() to compare strings ignoring case [OK]
      Common Mistakes:
      • Using equals() when ignoring case is needed
      • Using == for string comparison
      • Using non-existent compareToIgnoreCase() method
      3. What will be the output of the following Java code?
      String a = "apple";
      String b = "banana";
      int result = a.compareTo(b);
      System.out.println(result > 0 ? "a > b" : "a <= b");
      medium
      A. a > b
      B. a <= b
      C. 0
      D. Compilation error

      Solution

      1. Step 1: Understand compareTo() behavior

        compareTo() returns a negative number if the first string is alphabetically before the second.
      2. Step 2: Compare "apple" and "banana" alphabetically

        "apple" comes before "banana", so a.compareTo(b) returns a negative value, making result > 0 false.
      3. Final Answer:

        a <= b -> Option B
      4. Quick Check:

        compareTo() negative means first string is less [OK]
      Hint: compareTo() negative means first string is alphabetically before second [OK]
      Common Mistakes:
      • Assuming compareTo() returns boolean
      • Confusing positive and negative return values
      • Expecting 0 when strings differ
      4. The following code is intended to print "Strings are equal" if two strings match ignoring case. What is wrong?
      String s1 = "Java";
      String s2 = "java";
      if (s1 == s2) {
      System.out.println("Strings are equal");
      } else {
      System.out.println("Strings are not equal");
      }
      medium
      A. Using == compares references, not content
      B. Missing semicolon after if condition
      C. Strings cannot be compared in Java
      D. Should use compareTo() instead of ==

      Solution

      1. Step 1: Identify how string comparison works

        Using == compares if both variables point to the same object, not if their text is equal.
      2. Step 2: Correct way to compare ignoring case

        Use s1.equalsIgnoreCase(s2) to compare string content ignoring case.
      3. Final Answer:

        Using == compares references, not content -> Option A
      4. Quick Check:

        Use equalsIgnoreCase() for content ignoring case [OK]
      Hint: Never use == for string content comparison [OK]
      Common Mistakes:
      • Using == instead of equalsIgnoreCase()
      • Thinking == compares string content
      • Using compareTo() for equality check
      5. You want to sort an array of strings alphabetically ignoring case. Which approach correctly compares two strings s1 and s2 for sorting?
      hard
      A. s1.compareToIgnoreCase(s2)
      B. s1.equalsIgnoreCase(s2)
      C. s1.compareTo(s2)
      D. s1 == s2

      Solution

      1. Step 1: Understand sorting with string comparison

        Sorting requires a method that returns an integer indicating order, not just equality.
      2. Step 2: Identify method ignoring case for order

        compareToIgnoreCase() compares strings alphabetically ignoring case and returns an int for sorting.
      3. Step 3: Confirm method availability

        Java's String class has compareToIgnoreCase() for this purpose.
      4. Final Answer:

        s1.compareToIgnoreCase(s2) -> Option A
      5. Quick Check:

        Use compareToIgnoreCase() for case-insensitive sorting [OK]
      Hint: Use compareToIgnoreCase() to sort strings ignoring case [OK]
      Common Mistakes:
      • Using equalsIgnoreCase() which returns boolean, not order
      • Using compareTo() which is case sensitive
      • Using == which compares references
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard