Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/topics/operator-overloading.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,15 @@ For the assignment operations, for example `a += b`, the compiler performs the f
| `a != b` | `!(a?.equals(b) ?: (b === null))` |

These operators only work with the function [`equals(other: Any?): Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/equals.html),
which can be overridden to provide custom equality check implementation. Any other function with the same name (like `equals(other: Foo)`) will not be called.
which you can override to provide a custom equality check implementation.
Any other function with the same name (like `equals(other: Foo)`) is ignored.

> `===` and `!==` (identity checks) are not overloadable, so no conventions exist for them.
> `===` and `!==` (identity checks) aren't overloadable, so no conventions exist for them.
>
{style="note"}

The `==` operation is special: it is translated to a complex expression that screens for `null`'s.
`null == null` is always true, and `x == null` for a non-null `x` is always false and won't invoke `x.equals()`.
Kotlin calls `.equals()` when neither operand is the `null` literal and the comparison isn’t between two floating-point types.
Otherwise, `===` is used for `null` literal checks, and non-null floating-point values are compared by numeric value.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of "the null literal" can we say "a null value"?
And "=== is used to check for null values" rather than "=== is used for null literal checks"?

And can you make the second sentence active? "Kotlin uses ===" or something like that?

Copy link
Contributor Author

@daniCsorbaJB daniCsorbaJB Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of "the null literal" can we say "a null value"?

🤔 It's an important difference here. With two expressions A and B, A == B expands to A?.equals(B) ?: (B === null) even with a null value. If either operand is literal null, A == B instead expands to A === B.

And can you make the second sentence active? "Kotlin uses ===" or something like that?

Sure thing 👍


### Comparison operators

Expand Down