Questions tagged [equals]

Refers to Java equals method, indicating whether some object is "equal to" this one.

General Introduction:

equals() method is defined in the java.lang.Object class, from which all other classes are derived, hence it's automatically defined for every class. However, it doesn't perform an intelligent comparison for user defined classes unless they override it. If it's not defined for a (user) class, it behaves the same as equality operator ==, which compares equality of references in Java.

The equals() method implements an equivalence relation on non-null object references:

  1. It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  2. It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  3. It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  4. It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  5. For any non-null reference value x, x.equals(null) should return false.

Hence, if any user defined class overrides equals() method then it should follow the above criteria for proper implementation.

Note that it is generally necessary to override the hashCode() method whenever this method is overridden, so as to maintain the general contract for the hashCode() method, which states that equal objects must have equal hash codes.


Further Reading:

  1. Javadoc for equals().

  2. Stackoverflow answers: Overriding equals and hashCode in Java , Java equals for a Class. Is == same as .equals,Java: equals and == and Should equals(Object) method be overridden when overriding hashCode() in java.

3150 questions
193
votes
28 answers

How to check if my string is equal to null?

I want to perform some action ONLY IF my string has a meaningful value. So, I tried this. if (!myString.equals("")) { doSomething } and this if (!myString.equals(null)) { doSomething } and this if ( (!myString.equals("")) &&…
Roman
  • 124,451
  • 167
  • 349
  • 456
187
votes
22 answers

Difference between null and empty ("") Java String

What is the difference between null and the "" (empty string)? I have written some simple code: String a = ""; String b = null; System.out.println(a == b); // false System.out.println(a.equals(b)); // false Both statements return false. It seems,…
Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
179
votes
5 answers

What is the difference between IEqualityComparer and IEquatable?

I want to understand the scenarios where IEqualityComparer and IEquatable should be used. The MSDN documentation for both looks very similar.
Tilak
  • 30,108
  • 19
  • 83
  • 131
161
votes
8 answers

Apache Commons equals/hashCode builder

I'm curious to know, what people here think about using org.apache.commons.lang.builder EqualsBuilder/HashCodeBuilder for implementing the equals/hashCode? Would it be a better practice than writing your own? Does it play well with Hibernate?…
aug70co
  • 3,965
  • 5
  • 30
  • 44
154
votes
8 answers

Overriding the java equals() method - not working?

I ran into an interesting (and very frustrating) issue with the equals() method today which caused what I thought to be a well tested class to crash and cause a bug that took me a very long time to track down. Just for completeness, I wasn't using…
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
140
votes
1 answer

bash string equality

In bash, what's the difference, if any, between the equal and double equal test operators? [[ "a" = "a" ]] && echo equal || echo not-equal [[ "a" == "a" ]] && echo equal || echo not-equal [[ "a" = "b" ]] && echo equal || echo not-equal [[ "a" == "b"…
brianegge
  • 29,240
  • 13
  • 74
  • 99
136
votes
11 answers

How to override equals method in Java

I am trying to override equals method in Java. I have a class People which basically has 2 data fields name and age. Now I want to override equals method so that I can check between 2 People objects. My code is as follows public boolean…
user702026
131
votes
21 answers

compareTo() vs. equals()

When testing for equality of String's in Java I have always used equals() because to me this seems to be the most natural method for it. After all, its name already says what it is intended to do. However, a colleague of mine recently told me had…
Thomas Lötzer
  • 24,832
  • 16
  • 69
  • 55
125
votes
6 answers

How default .equals and .hashCode will work for my classes?

Say I have my own class public class MyObj { /* ... */ } It has some attributes and methods. It DOES NOT implement equals, DOES NOT implement hashCode. Once we call equals and hashCode, what are the default implementations? From Object class? And…
alexeypro
  • 3,633
  • 7
  • 36
  • 49
111
votes
8 answers

How should equals and hashcode be implemented when using JPA and Hibernate

How should model class's equals and hashcode be implemented in Hibernate? What are the common pitfalls? Is the default implementation good enough for most cases? Is there any sense to use business keys? It seems to me that it's pretty hard to get it…
egaga
  • 21,042
  • 10
  • 46
  • 60
105
votes
14 answers

Is there a Java utility to do a deep comparison of two objects?

How to "deep"-compare two objects that do not implement the equals method based on their field values in a test? Original Question (closed because lack of precision and thus not fulfilling SO standards), kept for documentation purposes: I'm trying…
Uri
  • 88,451
  • 51
  • 221
  • 321
101
votes
3 answers

Is there a Java standard "both null or equal" static method?

To save some typing and clarify my code, is there a standard version of the following method? public static boolean bothNullOrEqual(Object x, Object y) { return ( x == null ? y == null : x.equals(y) ); }
Chris Conway
  • 55,321
  • 43
  • 129
  • 155
101
votes
5 answers

Why does "true" == true show false in JavaScript?

MDC describes the == operator as follows: If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible;…
Isaac
  • 1,677
  • 3
  • 15
  • 22
96
votes
1 answer

double equals vs is in python

I run the following in the Python interpreter: >>> foo = 10 >>> dir(foo) == dir(10) True >>> dir(foo) is dir(10) False >>> Why is this?
ben
  • 1,583
  • 2
  • 11
  • 12
91
votes
16 answers

Compare two objects with .equals() and == operator

I constructed a class with one String field. Then I created two objects and I have to compare them using == operator and .equals() too. Here's what I've done: public class MyClass { String a; public MyClass(String ab) { a = ab; …
Fastkowy
  • 1,285
  • 3
  • 15
  • 16