
A possible requirement would be that "A = B if and only if all operations on objects A and B will have the same result", in addition to reflexivity, symmetry, and transitivity. For example, 1/2 and 2/4 are considered equal when seen as a rational number.
Some other tailor-made equality, preserving the external behavior. A simple way to achieve this is through representational equality: checking that the values have the same representation. which may be either shallow (testing only immediate subparts), or deep (testing for equality of subparts recursively). Structural equality (that is, their contents are the same). Content equality: if the objects referenced by two references (A and B) are equivalent in some sense:. Interactions with the object through A are indistinguishable from the same interactions through B, and in particular changes to the object through A are reflected through B. Location equality (identity): if two references (A and B) reference the same object. In such languages, there becomes a need to test for two different types of equality: In many modern programming languages, objects and data structures are accessed through references. two different references to the same object, e.g., two nicknames for the same person. two objects being equal but having different representation, e.g., a $1 bill and a $1 coin. two objects being equal but distinct, e.g., two $10 banknotes. two different objects of the same type, e.g., two hands. It is often necessary to distinguish between: Sometimes, particularly in object-oriented programming, the comparison raises questions of data types and inheritance, equality, and identity. One possible meaning of equality is that "if a equals b, then either a or b can be used interchangeably in any context without noticing any difference." But this statement does not necessarily hold, particularly when taking into account mutability together with content equality. It is used in switch statements to dispatch the control flow to the correct branch, and during the unification process in logic programming. It is used to test if an element already exists in a set, or to access to a value through a key. 5.2 Confusion with assignment operatorsĮquality is used in many programming language constructs and data types. 1.3 Comparing values of different types. Relational operators can be seen as special cases of logical predicates.
In languages such as C, relational operators return the integers 0 or 1, where 0 stands for false and any non-zero value stands for true.Īn expression created using a relational operator forms what is termed a relational expression or a condition.
In programming languages that include a distinct boolean data type in their type system, like Pascal, Ada, or Java, these operators usually evaluate to true or false, depending on if the conditional relationship between the two operands holds or not.
These include numerical equality ( e.g., 5 = 5) and inequalities ( e.g., 4 ≥ 3). In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. JSTOR ( March 2022) ( Learn how and when to remove this template message).Unsourced material may be challenged and removed.įind sources: "Relational operator" – news Please help improve this article by adding citations to reliable sources. This article needs additional citations for verification.