0% found this document useful (0 votes)
40 views

Core Java Interview Questions and Answers (2024) - InterviewBit

The document discusses the difference between the equals() method and the equality operator (==) in Java. It notes that (==) compares memory locations while equals() compares character values. An example is provided where (==) returns true for two identical String variables because they reference the same memory location, unlike equals() which would compare each character. More detailed information on this topic can be found in Question 73 on the Interviewbit site.

Uploaded by

ty cse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Core Java Interview Questions and Answers (2024) - InterviewBit

The document discusses the difference between the equals() method and the equality operator (==) in Java. It notes that (==) compares memory locations while equals() compares character values. An example is provided where (==) returns true for two identical String variables because they reference the same memory location, unlike equals() which would compare each character. More detailed information on this topic can be found in Question 73 on the Interviewbit site.

Uploaded by

ty cse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

3/14/24, 5:26 AM Core Java Interview Questions and Answers (2024) - InterviewBit

Online Free New


Practice Resources  Contests   Events Scaler S
IDE Mock

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 -

String str1 = "InterviewBit";


String str2 = "InterviewBit";

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

Instructions from Interviewbit

Start Test

https://www.interviewbit.com/java-interview-questions/ 2/2

You might also like