mRt
mRt

Reputation: 1223

Java reimplementing the ==

I know, for instance that, in Python, if I reimplement the method __ cmp __ I am modifying the behaviour of ==. I thought that the same thing could be done in Java, reimplementing equals (like, when you reimplement toString, it affects the print), but... No, or, I don't know how (I searched in google and it seems that, you couldn't) Am I right?? equals does not affect the ==?? If so, what's the point of equals?? Thanks

Upvotes: 4

Views: 194

Answers (5)

Alexis Dufrenoy
Alexis Dufrenoy

Reputation: 11946

It can't be done. That's one C++ feature Java didn't inherited...

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 993183

Python's == operator is the same as .equals() in Java. You can override this with .__cmp__() in Python and .equals() in Java.

Python's is operator is the same as == in Java, and neither of these can be overridden.

Upvotes: 6

Codemwnci
Codemwnci

Reputation: 54884

== is a comparator

.equals() is a method

The need for equals is because comparing objects is not as straight forward as doing a simple comparison.

If you say object1 == object2

Then, the result is only true if they point to the same memory space (i.e. they reference the SAME object).

If however, you want to check that the attributes, or even a subset of attributes of an object are the same, then you would implement your own equals method, and specify what constitutes two objects being equal.

So, the answer is, what do you define as equal?

  • Two objects that reference the same object in memory? Then use ==
  • or two objects that contain the same data. Then use .equals()

Upvotes: 3

Amir Afghani
Amir Afghani

Reputation: 38531

== compares references, not values. The use of == with object references is generally limited to the following:

  1. Comparing to see if a reference is null.
  2. Comparing two enum values. This works because there is only one object for each enum constant.
  3. You want to know if two references are to the same object

a.equals(b) compares values for equality. Because this method is defined in the Object class, from which all other classes are derived, it's automatically defined for every class. However, it doesn't perform an intelligent comparison for most classes unless the class overrides it. It has been defined in a meaningful way for most Java core classes. If it's not defined for a (user) class, it behaves the same as ==.

Upvotes: 1

Jeffrey Hantin
Jeffrey Hantin

Reputation: 36504

The operator == compares object references for equality. The equals method is intended to perform value comparison -- for example, two distinct String objects that represent the same sequence of characters will compare equal if you use equals, but not if you use ==.

As far as I know, operator overloading was left out of Java as a matter of language design. (Why the language designers built in an overload for + over String boggles my mind. Convenient, yes, but IMO that's cheating.)

Upvotes: 5

Related Questions