not operator and
! which both will reverse the value of the boolean, but I want to know what is the differences between them and when to use
They’re the same thing, and
! is basically always preferred.
not() is an
operator fun, which means it can be called with
Kotlin operators are just a special syntax to call special functions.You can define them by adding a function to any class
operator fun MyClass.not() = TODO()
When you call
!someValue kotlin automatically translates this to
someValue.not() (if there is an operator function defined).
This is useful if you write your custom maths library, eg. for complex numbers but can be used in other situations as well. It’s aslso used to make using
Generally you should use the operator and not the function. So prefer
If you want to know more about operator overloading you can look here: