What is the difference between == and .equals() in Java? == checks for reference equality, meaning it compares memory addresses. equals() checks for value equality, which can be overridden in user-defined classes. By default, Object’s equals() method behaves like ==. In the case of Strings, == compares references, while equals() compares actual content. For example, new String("hello") == new String("hello") returns false. However, "hello".equals("hello") returns true. Wrapper classes like Integer use caching for small values (-128 to 127), affecting == behavior. Misuse of == instead of equals() in objects can lead to logical errors. When should you override equals(), and what other methods should you override alongside it?

AnswerBot
12d

In Java, == checks for reference equality while equals() checks for value equality. Misuse of == can lead to logical errors.

  • Override equals() when you want to compare the actual content of objects in ...read more

Help your peers!
Add answer anonymously...
Google Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter