Equality and relational operators
The following table lists the meanings of equality and relational operators.
Operator | Meaning |
---|---|
== | Equal; see discussion below |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
To test whether two objects x and y represent the same thing, use the==
operator. (In the rare case where you need to know whether twoobjects are the exact same object, use the identical()function instead.) Here’s how the ==
operator works:
If x or y is null, return true if both are null, and false if onlyone is null.
Return the result of the method invocation
x.==(y)
. (That’s right,operators such as==
are methods that are invoked on their firstoperand. You can even override many operators, including==
, asyou’ll see inOverridable operators.)
Here’s an example of using each of the equality and relationaloperators:
assert(2 == 2);
assert(2 != 3);
assert(3 > 2);
assert(2 < 3);
assert(3 >= 3);
assert(2 <= 3);