-3

I read that

usually we use equals() for comparing immutable object, where == is used for mutable object

I know the differences between equals and "==", but why it is preferred to use equals for immutable objects? and why using "==" for mutable objects? since in my logic when I want to compare two objects (compare state, not identity) so why it is not recommended to use equals()?

Matt. Stroh
  • 904
  • 7
  • 16
  • 4
    where you read that? – ByeBye Feb 18 '17 at 16:00
  • 3
    Yeah that's complete BS. You should be more careful about what you read. Clearly you *don't* understand the difference between `==` and `equals()` if you don't see anything wrong with what you've written. – Kayaman Feb 18 '17 at 16:01
  • `==` compares **references** , when used with objects. It doesn't matter whether object is mutable or immutable – dumbPotato21 Feb 18 '17 at 16:02
  • I read it in my lecture slides. I know that "==" is used for reference equality, this is why I ask it here, since I didn't get the logic behind this sentence – Matt. Stroh Feb 18 '17 at 16:09
  • *"I read it in my lecture slides."* then these slides are wrong. Or your slides have a point beyond "comaring mutables" which makes comparing references somehow usefull... – Timothy Truckle Feb 18 '17 at 16:16
  • It is quite strange how often this topic comes up, equals or == for String. So easily googled. – Jack Flamp Feb 18 '17 at 16:25
  • 1
    You need to ask your instructor what was meant by that sentence, because it's absolute garbage with regards to Java. It's *specious* even if I consider languages that allow *operator overloading*. – Elliott Frisch Feb 18 '17 at 16:28
  • according to this link (MIT) [link](http://web.mit.edu/6.005/www/fa14/classes/09-af-ri-equality/) in the paragraph "The Final Rule for equals and hashCode()" it also says that "mutable types should not override equals() and hashCode() at all, and should simply use the default implementations provided by Object." – Matt. Stroh Feb 19 '17 at 11:51
  • Contrary to many comments above, your textbook/instructor is sort of correct but failed to specify the constraints governing that statement. People are conflating the issues of reference equality vs instance equality and instance equivalence and instance value identity. Java doesn't differentiate at the language level between equality and equivalence. This question comes up frequently when dealing with Sets of mutable objects, and it is why you should avoid using mutable objects as Map keys. To understand the subtlety read the source of HashMap. – Dave Jul 08 '21 at 07:35

1 Answers1

4

equals is always used to compare the values held by objects. It's used to compares values of immutable objects such as String or Integer or mutable objects like List or Map among others.

There is no question of mutable or immutable objects when it comes to comparing for equality of two objects using equals.

On the other hand, the == symbol purely compares whether two references are the same or pointing to the same object.


Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31