Overview of comparisons
In general, the comparison operators work as you might expect if you are familiar with most programming languages. Some notable differences are:
- The operators
>,<,>=, and<=operators work with all objects that implement theComparableinterface, not just with numbers. - The standard equal comparison
==operator implicitly uses the equals method on the first (leftmost) object. This operator does not check for pointer equality. It isnullsafe in that if the value on either side of the operator isnull, Gosu does not throw a null pointer exception.Note: In contrast, in the Java language, the==operator evaluates totrueif and only if both operands have exactly the same reference value. The Java expression evaluates totrueif both terms refer to the same object in memory. This behavior provides the expected result for primitive types like integers. For reference types, the references are not usually what you want to compare. Instead, to compare value equality, Java code typically uses object.equals(), rather than the==operator. - In some cases, you do need to
compare identity references, to determine whether two objects reference
the same in-memory object. Gosu provides a special equality operator
called
===(three equal signs) to compare object equality. This operator compares whether both references point to the same in-memory object. The following examples illustrate some differences between==and===operators:
Expression |
Output |
Description |
|---|---|---|
|
|
These two comparison
arguments reference the same value but different objects. Using the double-equals
operator returns |
|
|
These two comparison
arguments reference the same value but different objects. Using the triple-equals
operator returns |
See also
