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
1
vote
1 answer

JUnit Test appears to not be working properly when comparing java.awt.Color objects

Using this piece of code: @Test public void testBallSize() { Ball b = new Ball(1f,1f,0.5f,0.5f, Color.WHITE); assertEquals("Color of the ball should be white.", Color.WHITE, b.getColor()); } Produced: java.lang.AssertionError: Color of…
Testko
  • 31
  • 5
1
vote
1 answer

Java: Invoking a Class-type Array to Test for Equality

I'm working on a program that first takes an even array of doubles and sequentially creates a second array of (x, y) coordinates. Then, an invoking PointArray has to be compared to the argument PointArray by comparing the x-coordinates and…
user7218814
1
vote
2 answers

C++ CPPUNIT_ASSERT with two parameters

In some code i found the following line: CPPUNIT_ASSERT(1, val.getBytefield().size()); Does this really compare the two parameters for equality? Normally, i would expect this comparison with CPPUNIT_ASSERT_EQUAL: CPPUNIT_ASSERT_EQUAL(1,…
Doenbot3000
  • 27
  • 1
  • 8
1
vote
3 answers

TCL 8.3: syntax error in expression "a eq b"

I have to run TCL code written for version 8.5 with the interpreter of version 8.3. I am having problems with eq. Seems 8.3 doesn't recognize it. A simple code: % expr { "a" eq "b" } returns an error message like: syntax error in expression "a eq…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
1
vote
0 answers

Hibernate buiseness key equals() doesn't work

I am using Hibernate to store data from parsing html using jsoup. Here are my entities: Sentence.hbm.xml
ssukienn
  • 558
  • 6
  • 22
1
vote
0 answers

SonarQube warning: use equals instead == : relevant?

SonarQube rules are enforced in my dev project, and I am not sure that the rule "S1698" ([MAJOR] Change this comparison to use the equals method) is relevant in several of my cases (and I still need to do something for these warnings). Do you see…
Fabimaru
  • 423
  • 3
  • 9
1
vote
1 answer

Which is faster for comparing case class object in scala : a) equals(==) method or b) equating hash values

Which is faster for comparing object in scala : a) comparing two case class objects using the equals(==) method or b) creating hash out of the member variables of object and then comparing that hash. I think equals method should be faster but my…
Vaibhavrtk
  • 62
  • 7
1
vote
0 answers

Java Hibernate Proxy N+1 Equals Id not bind?

I have a implementation of Object->equals like this i know not maybe the best implementation of it but this is our legacy method. @Override public boolean equals(final Object obj){ if(this == obj){ return true; } if(obj ==…
chiperortiz
  • 4,751
  • 9
  • 45
  • 79
1
vote
3 answers

Custom equals outside of the class definition

Is there a way to write a custom equals method compactly when trying to compare two objects but not relying on those objects' internal equals() method? For example, if I had two Foo objects like so: public class Foo { int id; String name; …
John Baum
  • 3,183
  • 11
  • 42
  • 90
1
vote
1 answer

create equal method for Java class to compare double or int values

My homework is to create an equal method of a class that override Object class's equal method in Java. I have coded like below but the lecturer commented that: "doubles cannot be compared for equality with ==,!= as storage is not exact." So how do I…
Fazan Cheng
  • 866
  • 1
  • 8
  • 13
1
vote
1 answer

Filter condition BIRT - equal

I'm trying to create a filter condition, to show all results when this field is equal to 30 OR to 31. I'm using this, but this is only showing for '30'.
blocnt
  • 73
  • 1
  • 8
1
vote
2 answers

Two objects being equal not regarded as distinct by LINQ

I'm comparing two objects of type Triangle and apparently they are deemed equal (I implemented my custom GetHaschCode as well as Equal method and operator). List triangles = ...; bool same = triangles[0] == triangles[1]; // same is…
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
1
vote
1 answer

scala: overriding equals of basic types

I've defined a wrapper class: class Wrapper[T](private val value: T) and I want to make sure that w(v1) == v2 and v2 == w(v1) iff v1 == v2. The first part is easy because you can override equals method of Wrapper class. But the problem is the other…
K J
  • 4,505
  • 6
  • 27
  • 45
1
vote
0 answers

Why StringBuilder equals() method is not overridden?

Can anyone explain to me why creators of java chose not to override Object equals() method in StringBuilder class? Thanks!
Zoran
  • 1,502
  • 19
  • 22
1
vote
1 answer

Difference between IN with <> (not equal) and NOT IN with = (equal)

I would like to know what is the difference between these two code blocks, because for me it looks like the same result will show up. We are looking for all records in the "commande" table that do not have an article with "stylo" in their…
A. Tamer
  • 11
  • 1