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

Equals override vs. IEquatable<>

For the life of me, I can't get my WPF binding to work correctly for a RibbonComboBox's SelectedItem property. Then I started reading about how .NET compares items. My understanding is that, in some cases, it compares the actual pointer. In which…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1
vote
1 answer

Writing a for loop in python that has the <= (smaller or equal) condition in it?

I want to iterate through different dates, for instance from 20/08/2015 to 21/09/2016, but I want to be able to run through all the days even if the year is the same. For instance 20/08/2015 to 25/09/2015. Now if I write this in C, I could just use…
SteveR
  • 13
  • 1
  • 4
1
vote
1 answer

Getting the names of all the instance fields used inside the equals function

I am working on a project related to hash functions in Java. The user is required to override the equals function and provide us with it. While overriding equals he may use a subset of all the instance fields of the class which I am considering as…
Kishan Kishore
  • 451
  • 6
  • 12
1
vote
2 answers

String contains method return false even when string contains substring

I have a string: 20160719_P_BID_20160718_130000 I need to check if this string contains the substring "BID". I have tried various methods like: .contains("BID") / .contains("_BID_") .indexOf("BID") .substring(11,14).equals("BID) but all…
user1889849
  • 49
  • 1
  • 7
1
vote
3 answers

Java Faster Equals method For float type

As I know set.add use equals method of FastFloat For me important only first two digits after point(!!!), so in the equals method to make equals faster I use Math.abs() >= 0.001 but I don't understand why this code return 2 instead of 1 because…
evgenii ershenko
  • 489
  • 1
  • 5
  • 14
1
vote
2 answers

String + String vs String + String returned from method

Today while working with String's i have encountered a behavior i don't know before. I'm not able to understand what's happening internally. public String returnVal(){ return "5"; } String s1 = "abcd5"; String s2 = "abcd"+"5"; …
SaiKiran V
  • 23
  • 2
1
vote
1 answer

PHP 7.0.7 hash_equals not working with crypt() or hash()?

I didnt see much on this even when searching google, google video & this website. Was wondering why this is if I set. Tried to do some tests on this though. Is mostly password_verify that I see though even on videos. wouldnt matter what I use if…
1
vote
4 answers

=== vs == , whats up?

Possible Duplicate: Javascript === vs == : Does it matter which “equal” operator I use? In PHP and Javascript code i'm seeing more === out there as opposed to what I'm used to == for equality. Is === just a fancy way to write == ? Whats the deal…
Chamilyan
  • 9,347
  • 10
  • 38
  • 67
1
vote
1 answer

How to appropriately override equals(Object o) while using jackson mapping

JSON : "ABCD": [ { "xyz": 3, "abc": 4, "date": { "start": 1462341600000, "stop": 1462513680000 } } ...similar sub parts ] Datatype defined : I have tried to…
Naman
  • 27,789
  • 26
  • 218
  • 353
1
vote
1 answer

Check If Set of Nodes are Equal to Each Other XPath 1.0

I want to use XPath to select a few nodes that end with the same word and check if they all equal one another. So, for example, 123456789 ... 123456789
jediwompa
  • 79
  • 1
  • 11
1
vote
1 answer

How do I compare LibGDX PolygonShapes?

How do I check if one PolygonShape is the same than an other one? .equals() does not work. This code does not print "equals". PolygonShape test1 = new PolygonShape(); PolygonShape test2 = new PolygonShape(); test1.setAsBox(10, 20, new Vector2(0,0),…
CodeBreaker
  • 162
  • 1
  • 10
1
vote
5 answers

Why java execution order is different each time & Value comparison is wrong

I am having doubt on System.err.print() execution control. The order in which statements are printed is differs for each execution. I do have 2 questions. Following is the code public class AutoBoxingProblems { public static void…
1
vote
1 answer

XHTML Validation Fatal Error (equal sign)

I've got an XHTML which contains a stylesheet link but does not work t all, because the Validator says this: Fatal Error: required character (found =) (expected ;) At line 10, column 69 =Raleway&subset=latin-ext' rel This works fine in HTML5. …
chuck
  • 63
  • 12
1
vote
2 answers

Override equals to check against class type or primitive int

I'm trying to figure out a way to have my class' equals method work for either the class type or a primitive int but something doesn't feel right about the approach I have: class MyClass { int myID; .... [other class variables/methods] …
VagrantC
  • 687
  • 2
  • 13
  • 31
1
vote
1 answer

volatile Thread object and thread object comparison in java

I am not much familiar with volatile keyword and thread. I have a piece of code that does comparison using ==. I got suggested these objects should be compared with equals. while(pollingThread == thisThread) where pollingThread defined as:private…
MonsterJava
  • 423
  • 7
  • 23