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
5649
votes
48 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,397
  • 4
  • 19
  • 15
1305
votes
15 answers

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

Two string variables are set to the same value. s1 == s2 always returns True, but s1 is s2 sometimes returns False. If I open my Python interpreter and do the same is comparison, it succeeds: >>> s1 = 'text' >>> s2 = 'text' >>> s1 is s2 True Why is…
jottos
  • 20,098
  • 9
  • 30
  • 27
845
votes
24 answers

How do I check for null values in JavaScript?

How can I check for null values in JavaScript? I wrote the code below but it didn't work. if (pass == null || cpass == null || email == null || cemail == null || user == null) { alert("fill all columns"); return false; } And…
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117
768
votes
26 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
  • 19,826
  • 28
  • 77
  • 115
723
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
  • 48,033
  • 60
  • 165
  • 247
628
votes
13 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
  • 45,296
  • 18
  • 54
  • 69
613
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
  • 8,775
  • 6
  • 33
  • 37
591
votes
8 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
  • 17,229
  • 26
  • 92
  • 141
535
votes
12 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
  • 38,661
  • 28
  • 100
  • 128
336
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ć
  • 4,188
  • 2
  • 22
  • 13
315
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
  • 57,719
  • 77
  • 200
  • 270
296
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,667
  • 2
  • 14
  • 17
293
votes
4 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
  • 91,433
  • 48
  • 218
  • 260
272
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,633
  • 3
  • 28
  • 20
254
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
  • 21,745
  • 17
  • 54
  • 63
1
2 3
99 100