Distinguish Java Reference Equality from equals

Quick Overview

Explain Java identity comparison, logical equality, and null-safe comparisons. Connect equals to hashCode and show why mutable equality fields can make hash-map keys unreliable.

Distinguish Java Reference Equality from equals

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

What is the difference between == and equals() in Java? Explain reference comparison, primitive comparison, and how a class defines logical equality. ### What a Strong Answer Covers Avoid describing object identity as a portable memory-address comparison. Explain Object's default equality, value-class overrides, null handling, and the hashCode obligation when equals is overridden. ### Follow-up Questions - Why can two distinct String instances compare equal with equals but not ==? - What can happen if a mutable field used by equals changes while the object is a hash-map key?

Overview: Explain Java identity comparison, logical equality, and null-safe comparisons. Connect equals to hashCode and show why mutable equality fields can make hash-map keys unreliable.

Jan 14, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

What is the difference between == and equals() in Java? Explain reference comparison, primitive comparison, and how a class defines logical equality.

What a Strong Answer Covers Guidance

Avoid describing object identity as a portable memory-address comparison. Explain Object's default equality, value-class overrides, null handling, and the hashCode obligation when equals is overridden.

Follow-up Questions Guidance

  • Why can two distinct String instances compare equal with equals but not ==?
  • What can happen if a mutable field used by equals changes while the object is a hash-map key?