Core Java Interview Questions and Answers (2024) - InterviewBit
Core Java Interview Questions and Answers (2024) - InterviewBit
11. Can you tell the difference between equals() method and equality operator (==) in
Java?
We are already aware of the (==) equals operator. That we have used this to compare the equality of the
values. But when we talk about the terms of object-oriented programming, we deal with the values in the form
of objects. And this object may contain multiple types of data. So using the (==) operator does not work in this
case. So we need to go with the .equals() method.
Both [(==) and .equals()] primary functionalities are to compare the values, but the secondary functionality is
different.
So in order to understand this better, let’s consider this with the example -
System.out.println(str1 == str2);
This code will print true. We know that both strings are equals so it will print true. But here (==) Operators
don’t compare each character in this case. It compares the memory location. And because the string uses the
constant pool for storing the values in the memory, both str1 and str2 are stored at the same memory location.
See the detailed Explanation in Question no 73: Link.
https://www.interviewbit.com/java-interview-questions/ 1/2
3/14/24, 5:26 AM Core Java Interview Questions and Answers (2024) - InterviewBit
Start Test
https://www.interviewbit.com/java-interview-questions/ 2/2