8

I know that equals() will compare the value of objects, the '==' operator will check if the variable point to the same memory.

I do not understand how equals() compare the value of objects, for example:

class Test { public Test(int x, float y) { this.x = x; this.y = y; } int x, float y; } Test test1 = new Test(1,2.0); Test test2 = new Test(1,2.0); 

So if I use equals(), will it compare each properties in each object?

And what about if we are talking about String? using equals() and operator “==”, do we still need to override the equals()?

3 Answers 3

20

No, if you don't override the equals-method in your class, then equals is the same as ==. See the documentation for this:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

The documentation also states what requirements there are for equals methods in case you want to implement it.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the equals method is inherited in this case from Object which has the following implementation: public boolean equals(Object obj){return this==obj;}
11

Not unless you overload it properly according to the rules laid down by Joshua Bloch.

The default behavior checks equality of references using ==.

It's important to override equals and hashCode for your objects, especially if you intend to use them in java.util.Collections.

15 Comments

Some random guy? I take it you don't know who Joshua Bloch is. The citation IS the standard. He's the guy who wrote the java.util Collections.
What if that 'random' individual actually conceived of and wrote the standard that you are citing? Does that make him silly?
@duffymo: Standards are a group effort. I'm sure there were many others involved.
@duffymo: I shall. You should try thinking once in a while too! Might help you get rid of your tendency to idolize people.
@duffymo @MattiVirkkunen Please take it elsewhere
|
0

The equals method is defined in class Object, and since all objects in Java implicitly or explicitly inherit from this class, they too will inherit the equals() method as implemented by Object. The default implementation in Object will simply return true if the objects pass the "==" condition.

However, you are free to override the equals() method in your own class and specify the criteria which must be checked to see if two objects are meaningfully equal. For example, you might say that two instances are only equal if each of its attributes contain the same values as another object, or you might instead want to simply check a few attributes which make up the objects "business key" and ignore the others.

The equality of String classes follow the same rules as any other class in Java; "==" will be true if they do refer to the same instance, and equals() will be true if they contain the same values.

1 Comment

All of the answers are saying the same thing. This is a basic concept of Java and you would do very well to understand it completely.