Does equals() method and == do the same thing in Java?

Options
- Yes
- No


CORRECT ANSWER : No

Discussion Board
equals() and ==

Both equals() method and "==" operator is used to compare objects to check equality. "==" operator works the same way for all objects whereas equals() method can be overridden and the logic can be changed as per the business rules. The main differences are as follows:

1. "==" compares both primitive and objects whereas equals() is only used for object comparison.
2. "==" is used to compare primitive data types e.g. boolean, int, char etc, while use equals() to compare objects in Java.
3. "==" have the return type as true if two reference are of same object whereas equals() method depends on overridden implementation.
4. To compare the string equals() method is used instead of "==" equality operator.

Rohit Sharma 08-10-2014 05:43 AM

equals() vs. ==

equals() is method and == is operator. Both are used to compare objects to check equality. Since equals() is method which can be overridden in Java and logic to compare objects can be changed based upon business rules. == is used to compare both primitive and objects. while equals() is only used for objects comparison.

Ravi Panse 07-5-2013 04:34 AM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement