Strings
String Comparison
Master identity vs value equality check protocols and lexicographical character order evaluations.
Interview: Focuses on == vs equals() checks, comparison boundaries, compareTo() sign values, and equalsIgnoreCase() overheads.
Because Strings are objects in Java, comparing them requires distinguishing between reference identity and value equality. Using the wrong comparison method can introduce logic bugs, especially when comparing string variables with pooled literals.
Core Idea
Use equals() to compare string values. Using == compares memory reference addresses, which fails for duplicate heap instances.
Why It Matters
String.equals() performs character-by-character checks, while compareTo() evaluates lexicographical order, returning positive, negative, or zero offsets.
Interview Lens
Evaluates == vs equals() references, compareTo() character difference calculations, and null-safe comparisons.
Comparison Operators and Methods
- Identity Operator (
==): Compares the memory addresses of the two references. Evaluates totrueonly if both references point to the exact same object on the heap. - Equality Method (
equals()): Performs a character-by-character comparison. Checks array lengths first, then compares elements. Returnstrueif both strings represent the same character sequence. - Lexicographical Check (
compareTo()): Compares strings alphabetically based on Unicode character values. Returns:0if the strings are equal.- A negative integer if the current string is lexicographically smaller than the argument.
- A positive integer if the current string is lexicographically larger.
Common Pitfalls
- Using == for Value checks: Comparing strings with
==, which might work occasionally due to JVM String Pooling but fails when strings are instantiated dynamically. - NullPointerExceptions (NPE): Calling
variable.equals("literal"). If the variable is null, it throws an NPE. - Assuming compareTo() returns fixed values: Assuming
compareTo()only returns-1,0, or1. It returns the difference between character values or lengths, which can be any integer.
Best Practices
- Always compare string values using
equals()orequalsIgnoreCase(). - Write null-safe comparisons by placing constant literals on the left side:
"literal".equals(variable), or useObjects.equals(a, b). - Use
compareTo()primarily for sorting and ordering operations.
Interview-Relevant Information
Q1: Why does comparing identical strings with == sometimes return true and sometimes false?
Answer: If both strings are literal references (e.g. "test"), the JVM returns the same cached reference from the String Pool, so == is true. If one string is created on the heap via new String() or dynamically concatenated at runtime, it is a different object at a different address, so == evaluates to false.
Q2: What is returned by "apple".compareTo("banana")?
Answer: It returns a negative value. Specifically, it compares the first mismatching characters ('a' and 'b'). The difference in their Unicode values ('a' - 'b') evaluates to -1.
Quick Checklist
Can you select the correct comparison method, write null-safe check statements, predict compareTo() differences, and avoid logic traps with ==? If yes, you understand string comparison.
Use Cases
Validating system passwords securely using value equality methods.
Sorting alphabetic list indices inside collection services.
Common Mistakes
Checking value conditions using == operators, causing logic bugs for dynamically created strings.
Calling equals() directly on nullable objects, causing runtime NullPointerExceptions.