Difference Between .equals() and = = in Java
By BYJU'S Exam Prep
Updated on: September 25th, 2023
The Difference Between .equals() and = = in Java is that the = = operator is used for comparing the reference or memory location of objects in a data heap, whether they are at the same location or not. In contrast, the String .equals() method in java compares the two given strings based on the string’s data. Both .equals() and = = in java are used to compare two objects.
Difference Between .equals() and = = in Java PDF
Here, we will discuss the complete exploration of the difference between equals and = = in Java, and then we will briefly explain equals and = = in Java. We are going to look into some more differences in various parameters below.
Table of content
Difference Between .equals() and = = in Java
One of the few differences between .equals() and = = in Java is that the = = is an operator, and .equals() is a method. The .equals() and = = are primarily used in Java to check the equality of two variables.
Key Differences Between .equals() and = = in Java
.equals() in Java |
= = in Java |
In Java, .equals() are considered a method |
In Java, == is considered an operator. |
It is used for content comparison. |
It is majorly used for address comparison. |
It is not possible to use the .equals() method with primitives. |
It is possible to use the == operator with objects and primitives. |
This method can compare the conflicting objects that utilize the equals() method and returns “false.” |
The == operator can not be used for comparing the conflicting objects, so the compiler shows the compile-time error at that moment. |
In javascript, the equals() method can be overridden. |
The equality operators(==) operator in javascript cannot be overridden. |
What is = = in Java?
In java, equality operators are denoted by the “= =” sign. These equality operators (= =) compare the reference or memory location of given data in a heap, whether these point to the same location or not.
Whenever we develop an object using the new operator, it will directly create a new memory location for that created object. So the “= =” operator is used to check memory location or address of two given objects is the same or not. These equality operators can be applied for every primitive type, including the boolean type. The equality operator “= =” can also be applied for object types.
What is .equals() in Java?
In Java, if we want to compare the two given strings, whether they are .equals() or not, then we use the string equals() method based on the provided data/content of the string. If the contents of both the strings are the same as each other in java, it returns true, or if all characters are not matched.