Equality expressions
Equality expressions return a Boolean value (true or false) indicating the result of
the comparison between the two expressions. Equality expressions consist
of the following types:
- Relational equality operator
(
==) - Object equality operator
(
===) - Inequality operator (
!=) - Object inequality operator
(
!==)
Relational equality operator (==)
The ==
operator tests for relational equality. The operands can be of any compatible
types. The result is always Boolean.
For object types, the following rules apply in the following order:
- If both objects implement the
Comparableinterface, the==operator calls theComparableinterface method compareTo. Gosu calls the compareTo method on the object to the left of the==operator. - Otherwise, the
==operator calls the native Java method object.equals() to compare values. Gosu calls the equals method on the object to the left of the==operator.
=== or !== operators.In the Java language, the == operator evaluates to true if and only if both operands
have exactly the same reference value, which refers to the same object
in memory. This behavior works well for primitive types like integers.
For reference types, you usually do not want to compare the object in
memory. Instead, to compare value equality, Java code typically uses
object.equals(), not the == operator. The object.equals() method includes the comparison
of object reference values, but also compares property values.
If you use the == operator for comparison of
reference types, Gosu calls object.equals(). In most cases, this
behavior is what you want for reference types.
Syntax
a == bExamples
Expression |
Result |
|---|---|
|
|
|
|
|
|
Object equality operator (===)
You compare identity references, rather
than the object values, to determine whether two objects reference the
same in-memory object. You use the Gosu operator === (three equal signs) to compare
object equality.
The operands can be of any compatible
types. The result type is always Boolean.
Examples comparing == and ===
Expression |
Output |
Description |
|---|---|---|
|
|
The two |
|
|
The two |
|
|
The two |
|
|
Gosu represents the
two |
|
|
These two variables
reference the same value but different objects, so the double-equals
operator returns |
|
|
These two variables
reference the same value but different objects, so the triple-equals
operator returns |
|
|
This example is harder to understand. By just looking at the code, it seems like these two |
|
|
The two |
Inequality operator (!=)
The !=
operator tests for relational inequality. The operands can be of any
compatible types. The result type is always Boolean.
For object types, the following rules apply in the following order:
- If both objects implement the
Comparableinterface, the!=operator calls theComparableinterface method compareTo to compare values. Gosu calls the compareTo method on the object to the left of the==operator. - Otherwise, the
!=operator calls the native Java method object.equals() to compare values. Gosu calls the equals method on the object to the left of the==operator.
=== or !== operators.Syntax
a != bExamples
Expression |
Result |
|---|---|
|
|
|
|
|
|
Object inequality operator (!==)
The !==
operator tests for object inequality. It returns the opposite value of
the === operator, which
tests for object equality not value equality.
The operands can be of any compatible
types. The result type is always Boolean.
Syntax
a !== bExamples comparing != and !==
Expression |
Output |
Description |
|---|---|---|
|
|
The two |
|
|
Gosu represents the
two |
|
|
These two variables
reference the same value but different objects. If you use the |
|
|
These two variables
reference the same value but different objects. If you use the |
|
|
The two |
|
|
The two |
|
|
This example is harder to understand. By just looking at the code, it seems like these two |
|
|
The two |
