The is operator in Kotlin

In Java, we use the instanceOf operator to check if an object is an instance of a certain class or interface? In Kotlin, we will use the is operator!

For example, I have an interface with 2 implementations as follows:

Now, if I instantiate a new Shape object with the implementation of Triangle, when checking if this new object is an instance of Rectangle using the is operator:

you will see the following result:


Conversely, to check that this object is not an instance of the Rectangle class, you can declare “!is” as follows:

Result:

Leave a Reply

Your email address will not be published. Required fields are marked *