Bird
Raised Fist0
Javaprogramming~10 mins

String comparison in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String comparison
Start
↓
Create two strings
↓
Compare strings using equals()
↓
If true
→Print "Strings are equal"
↓
If false
→Print "Strings are not equal"
↓
Compare strings using ==
↓
If true
→Print "References are equal"
↓
If false
→Print "References are not equal"
↓
End
This flow shows creating two strings, comparing their content with equals(), then comparing their references with ==, and printing results accordingly.
Execution Sample
Java
String s1 = new String("hello");
String s2 = new String("hello");
if (s1.equals(s2)) {
    System.out.println("Strings are equal");
} else {
    System.out.println("Strings are not equal");
}
if (s1 == s2) {
    System.out.println("References are equal");
} else {
    System.out.println("References are not equal");
}
This code compares two different String objects with the same content using equals() and ==, printing the results.
Execution Table
StepActionEvaluationResultOutput
1Create s1s1 = new String("hello")s1 points to new String object with "hello"
2Create s2s2 = new String("hello")s2 points to different new String object with "hello"
3Compare s1.equals(s2)s1.equals(s2) checks content equalitytrueStrings are equal
4Compare s1 == s2s1 == s2 checks reference equalityfalseReferences are not equal
5EndNo more code
💡 Execution stops after printing both comparison results.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
s1nullReference to String object "hello"Reference to String object "hello"Reference to String object "hello"
s2nullnullReference to different String object "hello"Reference to different String object "hello"
Key Moments - 2 Insights
Why does s1.equals(s2) return true but s1 == s2 return false?
equals() compares the actual text inside the strings, which is the same, so it returns true. The == operator compares if both variables point to the exact same object in memory, which they do not, so it returns false. See execution_table rows 3 and 4.
What would happen if we used String s1 = "hello"; and String s2 = "hello"; instead?
Both s1 and s2 would point to the same string in the string pool, so s1 == s2 would be true. This is different from using new String().
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of s1.equals(s2) at step 3?
Afalse
Btrue
Cnull
Dthrows error
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 3 in the execution_table.
At which step does the program print "References are not equal"?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Output' column in the execution_table for the step printing reference comparison.
If s2 was assigned as s2 = s1; how would the output change?
ABoth prints would say equal
BBoth prints would say not equal
COnly equals() print would change
DOnly == print would change
💡 Hint
Refer to variable_tracker to see what happens when both variables point to the same object.
Concept Snapshot
String comparison in Java:
- Use equals() to compare text content.
- Use == to compare if two variables point to the same object.
- new String() creates new objects, so == is usually false.
- String literals may share references, making == true.
- Always use equals() to check string equality.
Full Transcript
This example shows how Java compares strings. Two strings s1 and s2 are created with the same text but as different objects. Using equals() compares their text and returns true, so the program prints "Strings are equal". Using == compares if they are the same object, which they are not, so it prints "References are not equal". This teaches that equals() checks content, while == checks references. If s2 was assigned s1, then both would point to the same object and both comparisons would be true.

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