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
80
votes
5 answers

Equals method for data class in Kotlin

I have the following data class data class PuzzleBoard(val board: IntArray) { val dimension by lazy { Math.sqrt(board.size.toDouble()).toInt() } } I read that data classes in Kotlin get equals()/hashcode() method for free. I instantiated two…
Vaibhav
  • 971
  • 1
  • 7
  • 8
80
votes
6 answers

Why does 1234 == '1234 test' evaluate to true?

Possible Duplicate: php == vs === operator An easy answer for someone I'm sure. Can someone explain why this expression evaluates to true? (1234 == '1234 test')
Cjueden
  • 1,200
  • 2
  • 13
  • 25
77
votes
3 answers

Operator Overloading with Interface-Based Programming in C#

Background I am using interface-based programming on a current project and have run into a problem when overloading operators (specifically the Equality and Inequality operators). Assumptions I'm using C# 3.0, .NET 3.5 and Visual Studio…
Zach Burlingame
  • 13,476
  • 14
  • 56
  • 65
77
votes
12 answers

Is it a bad idea if equals(null) throws NullPointerException instead?

The contract of equals with regards to null, is as follows: For any non-null reference value x, x.equals(null) should return false. This is rather peculiar, because if o1 != null and o2 == null, then we have: o1.equals(o2) // returns…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
76
votes
9 answers

Java assignment operator execution

In Java, I understand that assignment evaluates to the value of the right operand, so statements like x == (y = x) evaluate to true. This code, however, outputs false. public static void main(String[]args){ String x = "hello"; String y =…
Sam
  • 1,542
  • 2
  • 13
  • 27
75
votes
4 answers

Java code related to equals method

I am practicing for an exam, and found a sample problem that I don't understand. For the following code, find what the output is: public class Test { private static int count = 0; public boolean equals(Test testje) { …
Dries Coppens
  • 1,085
  • 9
  • 20
75
votes
10 answers

Overriding equals() & hashCode() in sub classes ... considering super fields

Is there a specific rule on how Overriding equals() & hashCode() in sub classes considering super fields ?? knowing that there is many parameters : super fields are private/public , with/without getter ... For instance, Netbeans generated equals() &…
wj.
  • 2,001
  • 3
  • 18
  • 19
72
votes
1 answer

Implementing GetHashCode correctly

I'd like to hear from the community on how I should go about implementing GetHashCode (or override it) for my object. I understand I need to do so if I override the equals method. I have implemented it a fair amount of times, sometimes just calling…
Zivka
  • 929
  • 1
  • 7
  • 7
72
votes
6 answers

Should I write equals() and hashCode() methods in JPA entities?

I want to check if entity is in a Collection member (@OneToMany or @ManyToMany) of another entity: if (entity2.getEntities1().contains(entity1)) { }
j2j
  • 821
  • 2
  • 9
  • 7
70
votes
5 answers

How to compare equality of lists of arrays with modern Java?

I have two lists of arrays. How do I easily compare equality of these with Java 8 and its features, without using external libraries? I am looking for a "better" (higher-level, shorter, more efficient) solution than brute-force code like this…
hyde
  • 60,639
  • 21
  • 115
  • 176
67
votes
6 answers

What makes reference comparison (==) work for some strings in Java?

I have following lines of code to compare String. str1 not equal to str2, which is understandable since it compares object reference. But then why is s1 equal to s2? String s1 = "abc"; String s2 = "abc"; String str1 = new String("abc"); String…
user84592
  • 4,750
  • 11
  • 55
  • 91
67
votes
11 answers

What's the difference between ".equals" and "=="?

I switched lecturers today and he stated using a weird code to me. (He said it's better to use .equals and when I asked why, he answered "because it is!") So here's an example: if (o1.equals(o2)) { System.out.println("Both integer objects are the…
OVERTONE
  • 11,797
  • 20
  • 71
  • 87
67
votes
8 answers

Using == operator in Java to compare wrapper objects

I'm reading SCJP Java 6 by Kathy Sierra and Bert Bates and this book is confusing me so much. On page 245 they state that the following code below. Integer i1 = 1000; Integer i2 = 1000; if(i1 != i2) System.out.println("different objects"); //Prints…
dido
  • 3,347
  • 11
  • 34
  • 42
66
votes
1 answer

How do you say not equal to in Ruby?

This is a much simpler example of what I'm trying to do in my program but is a similar idea. In an, if statement how do I say not equal to? Is != correct? def test vara = 1 varb = 2 if vara == 1 && varb != 3 puts "correct" else puts…
Ger Crowley
  • 901
  • 2
  • 10
  • 10
65
votes
3 answers

Groovy different results on using equals() and == on a GStringImpl

According to the Groovy docs, the == is just a "clever" equals() as it also takes care of avoiding NullPointerException: Java’s == is actually Groovy’s is() method, and Groovy’s == is a clever equals()! [...] But to do the usual equals()…
Anuj Arora
  • 3,017
  • 3
  • 29
  • 43