Computer Science, asked by Thusharabuddy8442, 1 year ago

Why objects are not equals ecen it hava same attributes in equals method

Answers

Answered by choudhary21
0

Explanation:

1) use == to compare primitive e.g. boolean, int, char etc, while use equals() to compare objects in Java.

2) == return true if two reference are of same object. Result of equals() method depends on overridden implementation.

3) For comparing String use equals() instead of == equality operator.

Answered by Anonymous
2

Answer:

If an object's hashcode is not the same as another object's hashcode, there is no reason to execute the equals() method: you just know the two objects are not the same. On the other hand, if the hashcode is the same, then you must execute the equals method to determine whether the values and fields are the same.

Can two objects which are not equal have the same hashCode?

YES, two objects, which are not equal to equals() method can still return same hashCode. By the way, this is one of the confusing bit of equals and hashcode contract.

Similar questions