Q9: What is the difference between == and the .equals() method?

  • == Operator: Compares primitive values directly, or checks reference identity (whether two reference variables point to the exact same memory address on the heap).
  • .equals() Method: Compares logical content equality when overridden by a class (e.g., String, Integer). The default implementation in Object uses == reference identity.
Key Takeaways
  • == compares memory references; .equals() compares logical values (if overridden).
  • Always override .hashCode() whenever overriding .equals().
Knowledge Check

What is the output of: new String("A").equals(new String("A"))?