- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check "instanceof" class in Kotlin?
Kotlin is a cross-platform, statistically typed, general-purpose programming language. It is very popular among the developer community because of its interoperable nature with JVM. In the programming world sometimes it is required to check the type of an object to implement a business logic.
Unlike Java, we don't have an "instance of" operator in Kotlin. However, we have an "is" operator in Kotlin for type checking and casting.
Example
The following example demonstrates how the "is" operator works in Kotlin.
fun main(args: Array<String>) { val x: String = "TutorialsPoint" // checking the instance and matching the type if(x is String){ println("The entered value is string") } else{ println("Invalid inputs ") } }
Output
It will match whether the value of variable "x" is a String or not and depending on that, it will generate the following output.
The entered value is string
We also have a "negate instance of" operator which is "!is". The following example shows how to use it.
fun main(args: Array<String>) { val x: String = "TutorialsPoint" if(x !is String){ println("The entered value is string") } else{ println("Invalid inputs ") } }
Output
"!is" will check whether the value of "x" is a String or not and depending on that, it will generate following output:
Invalid inputs
- Related Articles
- How to check generic type in Kotlin?
- How to use the TextWatcher class in kotlin?
- How to use the instanceof operator in Java?
- Extend data class in Kotlin
- How to Check if Android EditText is empty in Kotlin?
- How to check if a string is empty in Kotlin?
- How to create an instance of an abstract class in Kotlin?
- Best way to null check in Kotlin
- Instanceof operator in JavaScript
- instanceof operator in Java
- instanceof Keyword in Java
- How to check Android Phone Model programmatically using Kotlin?
- Java instanceof operator
- How to Check available space on Android devices using Kotlin?
- How to check if a "lateInit" variable has been initialized in Kotlin?
