Difference between == and equals() method

๐Ÿ’ก Concept: == Operator vs equals() Method

The == operator checks reference equality, while the equals() method checks logical equality of objects in Java.

๐Ÿ“˜ Quick Intro

== compares memory addresses; equals() compares object content. By default, equals() behaves like == unless overridden.

๐Ÿง  Analogy

Think of == as checking if two people live in the same house (same address), whereas equals() checks if two people have the same characteristics.

๐Ÿ”ง Technical Explanation

  • == compares references (memory addresses).
  • equals() compares object data, usually overridden for logical comparison.
  • For primitives, == compares values directly.
  • Strings override equals() to compare character sequences.
  • Custom classes should override equals() for proper logical equality.

๐ŸŽฏ Use Cases

  • โœ… Use == to check if two references point to the same object.
  • โœ… Use equals() to check if two objects are logically equal.
  • โœ… Override equals() in custom classes to define logical equality.

๐Ÿ’ป Example: Using == vs equals()


String a = new String("test");
String b = new String("test");

System.out.println(a == b); // false, different objects
System.out.println(a.equals(b)); // true, same content

โ“ Interview Q&A

Q1: What does == operator compare?
A: Reference equality (memory address).

Q2: What does equals() method compare?
A: Logical equality of object content.

Q3: Can equals() be overridden?
A: Yes, to define custom equality.

Q4: How do Strings compare with ==?
A: Checks if same object reference.

Q5: How do Strings compare with equals()?
A: Checks if character sequences are equal.

Q6: Is == suitable for primitives?
A: Yes, compares values directly.

Q7: What is the default equals() behavior?
A: Same as == unless overridden.

Q8: Can == be used to compare different object types?
A: Yes, but usually false.

Q9: Why override equals()?
A: To compare logical equality, not references.

Q10: What else should be overridden with equals()?
A: hashCode() method.

๐Ÿ“ MCQs

Q1. What does == compare?

  • Value equality
  • Reference equality
  • Type equality
  • Hash equality

Q2. What does equals() compare?

  • Reference equality
  • Logical equality
  • Memory address
  • Object type

Q3. Can equals() be overridden?

  • No
  • Yes
  • Sometimes
  • Never

Q4. How do Strings compare with ==?

  • Checks reference
  • Checks content
  • Checks hash
  • Throws error

Q5. How do Strings compare with equals()?

  • Checks content
  • Checks reference
  • Checks type
  • Throws error

Q6. Is == suitable for primitives?

  • No
  • Yes
  • Sometimes
  • Never

Q7. What is default equals() behavior?

  • Overrides reference
  • Same as ==
  • Throws error
  • Depends on JVM

Q8. Can == compare different object types?

  • No
  • Yes
  • Sometimes
  • Never

Q9. Why override equals()?

  • Reference equality
  • Logical equality
  • Performance
  • Memory

Q10. What else to override with equals()?

  • toString()
  • hashCode()
  • compareTo()
  • finalize()

๐Ÿ’ก Bonus Insight

Understanding == vs equals() is critical for Java developers to avoid bugs in object comparisons and collections usage.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup