☕ Java Beginner

What is the difference between == and .equals() in Java?

Why Interviewers Ask This

This is a classic screening question for Java roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.

Answer

In Java, == compares references (memory addresses) for objects — it checks if two variables point to the exact same object in memory. For primitives, it compares values directly. .equals() compares content/value — it checks if two objects are logically equal. For example, two different String objects with the value "hello" would return false with == (different objects) but true with .equals() (same content). Always use .equals() when comparing object content. Override equals() in custom classes to define meaningful equality.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Java codebase.