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
0 answers

How can you represent two equal enum constants as not being equal when semantically different?

I'm currently working on a tic-tac-toe game, and I keep track of the cells' values using a State enum. protected static enum State { BLANK, X, O; @Override public String toString() { if (this.name().equals("BLANK")) …
BrainFRZ
  • 437
  • 3
  • 15
1
vote
4 answers

Comparing Objects in java fails

I have just started to work a little with Java and trying to compare two objects with each other by overriding the equals method. I've been debugging this for quite a while trying to understand why it keeps returning false. I've stripped it down to…
Joakim Hansson
  • 544
  • 3
  • 15
1
vote
2 answers

Java Hash Code Implementation with Multiple equals and if

From what i know, Every equals object must have same hash code. However what if in equals method have multiple if that need to be followed ? Location is an object, Junction is an object, length is an integer, offset is an integer, section is an…
Rifqi Ryan
  • 19
  • 1
1
vote
2 answers

Unable to understand: If either of the specified arrays contain themselves as elements, the behavior of this method is undefined

I'm currently reading Java API on Arrays class and for deepEquals(Object[] a1, Object[] a2) method, I have encountered the following phrase: If either of the specified arrays contain themselves as elements either directly or indirectly through…
Thor
  • 9,638
  • 15
  • 62
  • 137
1
vote
3 answers

Java - Overriding hashCode and toString

When two objects have same value of ele in class A then those two objects are equal. So I have overridden toString and hashCode to return the object's ele (not considering the value of s anywhere). public class A { private int ele; private…
Exuberant
  • 189
  • 2
  • 13
1
vote
0 answers

Drools FactException - Rule Matching on Fact that Has Already Been Deleted

I'm running into a bizarre situation where a Fact that has been deleted is getting matched by a rule downstream. This seems to only happen when these conditions are met in this exact order: Multiple logically unequal Facts (via the equals() method)…
Emily
  • 145
  • 2
  • 8
1
vote
1 answer

Compare Two Structs Using Standard Method (Byte Comparison/Reflection) Even If Equals Method Exists

I have some simple struct, which overrides the Equals() method: public struct Pair { public Pair(T x, T y) { X = x; Y = y; } public T X { get; } public T Y { get; } public override bool Equals(object obj) { …
Mikkel R. Lund
  • 2,336
  • 1
  • 31
  • 44
1
vote
1 answer

is there any meaningful difference between equals(Object o) and equals(Name o)?if yes, what implication does it have?

I saw two different version this code, one from Java Oralce and the other from Youtube. The only difference between the two version is the parameter type of equals() method. One of the is equals(Object o) whereas the other is equals(Name o). I was…
user5849987
1
vote
5 answers

Using "OR" operator in Java

This is the code. do { System.out.println("Enter Product Code: "); medicine = machine1.nextInt(); if (medicine==1) { machine1.buy1(); machine1.addproduct(); } if (medicine==2); { machine1.buy2(); …
John
  • 47
  • 6
1
vote
5 answers

Comparing class with class instances without hashCode and equals

Is it possible to compare Class that contains some other class instances , that does not contain hashCode and equals We have 3 classes : Car.java , SUV.java and TestFinal.java Car.java (Simple POJO without hashCode and equals) public class Car { …
user1993412
  • 802
  • 2
  • 15
  • 29
1
vote
1 answer

Regular Expressions \w character class and equals sign

I am creating a regular expression to match the string @servername:port:databasename and through https://regex101.com/ I came up with \@(((\w+.*-*)+)?\w+)(:\d+)(:\w+) which matches e.g. @CORA-PC:1111:databasename or…
cora
  • 13
  • 2
1
vote
1 answer

Expression expected after this token

Why is this popping up? Syntax error on token "+", Expression expected after this token for (int row = 0; row < data.length; row++) { for (int col = 7;;) { data[row][col] = [row][1] + [row][2] + [row][3] + [row][4] + [row][5] +…
1
vote
2 answers

Gethashcode() function

Why aren't C1 and c2 have the same hashcode ? the code doesn't get to "Same".... ( i=0 in both classes) class myclass { public static int i; static void Main() { myclass c1 = new myclass(); myclass c2 = new myclass(); …
Rodniko
  • 4,926
  • 20
  • 69
  • 93
1
vote
1 answer

How does IEEE 754 define equal?

How does IEEE-754 define equal for "regular" double floating point numbers? Is the java implementation of Double.equal in accordance with IEEE-754? If I leave out the special values like NaN, -0, etc. are IEEE-754 double floating numbers equal, if…
Oliver Meyer
  • 413
  • 6
  • 7
1
vote
2 answers

How can I override equals() method using inheritance?

I have a subclass called "worker" extending the "Person" class. I am trying to override the equals() method from "Person" within the subclass of "Worker". Can anyone explain if my attempt is correct in terms of a basic override? public class Person…
Peanutcalota
  • 135
  • 1
  • 8
1 2 3
99
100