Questions tagged [equality]

Equality is a relationship between two or more items or variables or objects that exists if (1) the items are the same item, variable, or object or (2) the items are different items, variables or objects but they have the same value. This tag should generally be used with programming language specific tags as well as other contextual tags such as database system. The post should include as much context about the equality test as is possible.

This tag is typically used for questions concerning how to determine if two objects or variables are either the same object or different objects with the same value. Questions almost always require a programming language tag and other tags for context.

Programming languages represent variables, objects, or other storage containers using different underlying mechanisms for the storage of the values of variables and objects. A test for equality is to compare some characteristic of two variables or objects to determine if the characteristic is the same.

This characteristic is typically either (1) the storage identifier such as a reference handle, memory address, etc. when checking if the two are the same object or (2) the characteristic is the value of the objects retrieved using a storage identifier when checking if the value of the two are the same value.

Different programming languages and different operating environments have differences in how equality is tested and what operators are used to express a test for equality. In some cases various conversions from underlying storage representations may be involved in testing for equality.

See What is the difference between equality and equivalence? as well as Equivalence relation and natural ordering on a class in Java

2095 questions
131
votes
2 answers

Python3 Determine if two dictionaries are equal

This seems trivial, but I cannot find a built-in or simple way to determine if two dictionaries are equal. What I want is: a = {'foo': 1, 'bar': 2} b = {'foo': 1, 'bar': 2} c = {'bar': 2, 'foo': 1} d = {'foo': 2, 'bar': 1} e = {'foo': 1, 'bar': 2,…
Marc Wagner
  • 1,672
  • 2
  • 12
  • 15
126
votes
3 answers

Correct way to override Equals() and GetHashCode()

I have never really done this before so i was hoping that someone could show me the correct what of implementing a override of Except() and GetHashCode() for my class. I'm trying to modify the class so that i can use the LINQ Except()…
Nugs
  • 5,503
  • 7
  • 36
  • 57
125
votes
11 answers

Test for equality among all elements of a single numeric vector

I'm trying to test whether all elements of a vector are equal to one another. The solutions I have come up with seem somewhat roundabout, both involving checking length(). x <- c(1, 2, 3, 4, 5, 6, 1) # FALSE y <- rep(2, times = 7) #…
kmm
  • 6,045
  • 7
  • 43
  • 53
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
122
votes
5 answers

Why can a Python dict have multiple keys with the same hash?

I am trying to understand the Python hash function under the hood. I created a custom class where all instances return the same hash value. class C: def __hash__(self): return 42 I just assumed that only one instance of the above class…
Praveen Gollakota
  • 37,112
  • 11
  • 62
  • 61
122
votes
9 answers

Checking if two Dates have the same date info

How can I check if two different date objects have the same date information(having same day, month, year ...)? I have tried "==", "===" and .equals but none seems to work.
Hellnar
  • 62,315
  • 79
  • 204
  • 279
108
votes
7 answers

What is the difference between eq?, eqv?, equal?, and = in Scheme?

I wonder what the difference is between those operations in Scheme. I have seen similar questions in Stack Overflow but they are about Lisp, and there is not a comparison between three of those operators. I am writing the different types of commands…
yrazlik
  • 10,411
  • 33
  • 99
  • 165
103
votes
9 answers

How to compare Enums in TypeScript

In TypeScript, I want to compare two variables containing enum values. Here's my minimal code example: enum E { A, B } let e1: E = E.A let e2: E = E.B if (e1 === e2) { console.log("equal") } When compiling with tsc (v 2.0.3) I get the…
John J. Camilleri
  • 4,171
  • 5
  • 31
  • 41
102
votes
5 answers

How to check if two vars have the same reference?

How can you check if two or more objects/vars have the same reference?
clarkk
  • 27,151
  • 72
  • 200
  • 340
102
votes
5 answers

Why does ("foo" === new String("foo")) evaluate to false in JavaScript?

I was going to start using === (triple equals, strict comparison) all the time when comparing string values, but now I find that "foo" === new String("foo") is false, and same with this: var f = "foo", g = new String("foo"); f === g; // false Of…
Michael Butler
  • 6,079
  • 3
  • 38
  • 46
101
votes
7 answers

C# .Equals(), .ReferenceEquals() and == operator

My understanding of these three was: .Equals() tests for data equality (for the lack of a better description). .Equals() can return True for different instances of the same object, and this is the most commonly overridden method. .ReferenceEquals()…
999999
  • 1,873
  • 3
  • 14
  • 20
98
votes
5 answers

Determining Date Equality in Javascript

I need to find out if two dates the user selects are the same in Javascript. The dates are passed to this function in a String ("xx/xx/xxxx").That is all the granularity I need. Here is my code: var valid = true; var d1 = new…
Jarred
  • 1,986
  • 5
  • 27
  • 42
97
votes
11 answers

How do you test functions and closures for equality?

The book says that "functions and closures are reference types". So, how do you find out if the references are equal? == and === don't work. func a() { } let å = a let b = å === å // Could not find an overload for === that accepts the supplied…
user652038
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
87
votes
0 answers

php == vs === operator

What is the difference between == and === in php. I am unsure when to use both. Updated note: So that it shows up in StackOverflow search, the difference between == and === is the same as the difference between != and !==.
user34537