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

1929 questions
5660
votes
49 answers

Which equals operator (== vs ===) should be used in JavaScript comparisons?

I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0 inside of an if statement. Is there a…
bcasp
  • 57,427
  • 4
  • 17
  • 14
1220
votes
15 answers

Why does comparing strings using either '==' or 'is' sometimes produce a different result?

I've got a Python program where two variables are set to the value 'public'. In a conditional expression I have the comparison var1 is var2 which fails, but if I change it to var1 == var2 it returns True. Now if I open my Python interpreter and do…
jottos
  • 19,028
  • 9
  • 28
  • 26
724
votes
23 answers

How do I compare strings in Java?

I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug. Is == bad? When should it and should it not be used? What's the difference?
Nathan H
  • 44,105
  • 54
  • 154
  • 235
680
votes
23 answers

What is the difference between == and equals() in Java?

I wanted to clarify if I understand this correctly: == is a reference comparison, i.e. both objects point to the same memory location .equals() evaluates to the comparison of values in the objects
brainydexter
  • 17,648
  • 25
  • 70
  • 108
629
votes
14 answers

Is there a difference between "==" and "is"?

My Google-fu has failed me. In Python, are the following two tests for equality equivalent? n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!' Does this hold true for objects where you would be comparing…
Bernard
  • 43,502
  • 18
  • 52
  • 69
570
votes
4 answers

String comparison in Python: is vs. ==

I noticed a Python script I was writing was acting squirrelly, and traced it to an infinite loop, where the loop condition was while line is not ''. Running through it in the debugger, it turned out that line was in fact ''. When I changed it to…
Coquelicot
  • 7,727
  • 4
  • 28
  • 35
567
votes
7 answers

What's the difference between equal?, eql?, ===, and ==?

I am trying to understand the difference between these four methods. I know by default that == calls the method equal? which returns true when both operands refer to exactly the same object. === by default also calls == which calls equal?... okay,…
denniss
  • 15,528
  • 26
  • 84
  • 135
467
votes
10 answers

Elegant ways to support equivalence ("equality") in Python classes

When writing custom classes it is often important to allow equivalence by means of the == and != operators. In Python, this is made possible by implementing the __eq__ and __ne__ special methods, respectively. The easiest way I've found to do this…
gotgenes
  • 34,337
  • 27
  • 93
  • 119
297
votes
7 answers

How do you test to see if a double is equal to NaN?

I have a double in Java and I want to check if it is NaN. What is the best way to do this?
Eric Wilson
  • 51,818
  • 71
  • 192
  • 262
294
votes
15 answers

Are == and != mutually dependent?

I'm learning about operator overloading in C++, and I see that == and != are simply some special functions which can be customized for user-defined types. My concern is, though, why are there two separate definitions needed? I thought that if a == b…
BarbaraKwarc
  • 2,511
  • 2
  • 10
  • 17
292
votes
16 answers

Compare object instances for equality by their attributes

I have a class MyClass, which contains two member variables foo and bar: class MyClass: def __init__(self, foo, bar): self.foo = foo self.bar = bar I have two instances of this class, each of which has identical values for foo…
Željko Živković
  • 3,728
  • 2
  • 18
  • 13
270
votes
16 answers

Best practices for overriding isEqual: and hash

How do you properly override isEqual: in Objective-C? The "catch" seems to be that if two objects are equal (as determined by the isEqual: method), they must have the same hash value. The Introspection section of the Cocoa Fundamentals Guide does…
Dave Dribin
  • 5,543
  • 3
  • 26
  • 18
261
votes
3 answers

Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?

Is it guaranteed that False == 0 and True == 1, in Python (assuming that they are not reassigned by the user)? For instance, is it in any way guaranteed that the following code will always produce the same results, whatever the version of Python…
Eric O Lebigot
  • 81,422
  • 40
  • 198
  • 249
230
votes
11 answers

How do you compare structs for equality in C?

How do you compare two instances of structs for equality in standard C?
Hans Sjunnesson
  • 20,322
  • 17
  • 51
  • 63
217
votes
8 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,146
  • 2
  • 17
  • 22
1
2 3
99 100