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
239
votes
9 answers

equals vs Arrays.equals in Java

When comparing arrays in Java, are there any differences between the following 2 statements? Object[] array1, array2; array1.equals(array2); Arrays.equals(array1, array2); And if so, what are they?
PandaConda
  • 3,396
  • 2
  • 20
  • 22
229
votes
4 answers

What's the difference between IEquatable and just overriding Object.Equals()?

I want my Food class to be able to test whenever it is equal to another instance of Food. I will later use it against a List, and I want to use its List.Contains() method. Should I implement IEquatable or just override Object.Equals()? From…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
212
votes
14 answers

Why is x == (x = y) not the same as (x = y) == x?

Consider the following example: class Quirky { public static void main(String[] args) { int x = 1; int y = 3; System.out.println(x == (x = y)); // false x = 1; // reset System.out.println((x = y) == x);…
John McClane
  • 3,498
  • 3
  • 12
  • 33
204
votes
4 answers

Determine if 2 lists have the same elements, regardless of order?

Sorry for the simple question, but I'm having a hard time finding the answer. When I compare 2 lists, I want to know if they are "equal" in that they have the same contents, but in different order. Ex: x = ['a', 'b'] y = ['b', 'a'] I want x == y to…
toofly
  • 2,167
  • 3
  • 15
  • 9
203
votes
7 answers

Python if not == vs if !=

What is the difference between these two lines of code: if not x == 'val': and if x != 'val': Is one more efficient than the other? Would it be better to use if x == 'val': pass else:
lafferc
  • 2,741
  • 3
  • 24
  • 37
193
votes
3 answers

In Objective-C, what is the equivalent of Java's "instanceof" keyword?

I would like to check whether an object (e.g. someObject) is assignable (cast-able) to a variable of another type (e.g. SpecifiedType). In Java, I can write: someObject instanceof SpecifiedType A related question is finding whether the runtime type…
Dimitris
  • 682
  • 3
  • 14
  • 19
193
votes
21 answers

What's wrong with using == to compare floats in Java?

According to this java.sun page == is the equality comparison operator for floating point numbers in Java. However, when I type this code: if(sectionID == currentSectionID) into my editor and run static analysis, I get: "JAVA0078 Floating point…
user128807
  • 10,447
  • 17
  • 53
  • 72
185
votes
21 answers

Comparing two collections for equality irrespective of the order of items in them

I would like to compare two collections (in C#), but I'm not sure of the best way to implement this efficiently. I've read the other thread about Enumerable.SequenceEqual, but it's not exactly what I'm looking for. In my case, two collections would…
mbillard
  • 38,386
  • 18
  • 74
  • 98
183
votes
9 answers

Difference between String#equals and String#contentEquals methods

What is the difference between the String#equals method and the String#contentEquals method?
Arathana
  • 1,839
  • 2
  • 11
  • 3
178
votes
15 answers

How can I compare Lists for equality in Dart?

I'm comparing two lists in Dart like this: main() { if ([1,2,3] == [1,2,3]) { print("Equal"); } else { print("Not equal"); } } But they are never equal. There doesn't seem to be an equal() method in the Dart API to…
Mark B
  • 2,870
  • 3
  • 20
  • 18
174
votes
6 answers

Why `null >= 0 && null <= 0` but not `null == 0`?

I had to write a routine that increments the value of a variable by 1 if its type is number and assigns 0 to the variable if not, where the variable is initially null or undefined. The first implementation was v >= 0 ? v += 1 : v = 0 because I…
Chungmin Lee
  • 2,320
  • 2
  • 18
  • 19
165
votes
5 answers

What's the difference between == and .equals in Scala?

What is the difference between == and .equals() in Scala, and when to use which? Is the implementation same as in Java? EDIT: The related question talks about specific cases of AnyVal. The more general case is Any.
Jus12
  • 17,824
  • 28
  • 99
  • 157
163
votes
7 answers

jQuery object equality

How do I determine if two jQuery objects are equal? I would like to be able to search an array for a particular jQuery object. $.inArray(jqobj, my_array);//-1 alert($("#deviceTypeRoot") == $("#deviceTypeRoot"));//False alert($("#deviceTypeRoot")…
Casebash
  • 114,675
  • 90
  • 247
  • 350
161
votes
8 answers

LINQ Select Distinct with Anonymous Types

So I have a collection of objects. The exact type isn't important. From it I want to extract all the unique pairs of a pair of particular properties, thusly: myObjectCollection.Select(item=>new { …
GWLlosa
  • 23,995
  • 17
  • 79
  • 116
140
votes
1 answer

bash string equality

In bash, what's the difference, if any, between the equal and double equal test operators? [[ "a" = "a" ]] && echo equal || echo not-equal [[ "a" == "a" ]] && echo equal || echo not-equal [[ "a" = "b" ]] && echo equal || echo not-equal [[ "a" == "b"…
brianegge
  • 29,240
  • 13
  • 74
  • 99