Is Operator (is and !is) in Kotlin
Definition
- Is operators are used to check if a variable is of a particular type or not(!is) in Kotlin.
- This operator is mainly used in when and if statements.
- This operator is equivalent to the "instanceOf" method in Java.
Syntax
1. is Operator
if(value is variable_type){
//Block of Code
}
For example,
if(value is String){
println("$value is a String")
}
2. !is Operator
if(value !is variable_type){
//Block of Code
}
For example,
if(value !is String){
println("$value is not a String")
}
Is and !is Operator Example Program in Kotlin
//Is Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
val name = "Little Drops"
val number = 5
println("Checking data type of : $name")
checkDataType(name)
println()
println("Checking data type of : $number")
checkDataType(number)
}
fun checkDataType(value : Any){
if(value is String){
println("$value is a String")
}
if(value !is String){
println("$value is not a String")
}
if(value is Int){
println("$value is an Int")
}
if(value !is Int){
println("$value is not an Int")
}
}
Sample Output
Checking data type of : Little Drops
Little Drops is a String
Little Drops is not an Int
Checking data type of : 5
5 is not a String
5 is an Int
Kotlin Operators
- Kotlin Operators Overview
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Logical Operators in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Comparison Operators in Kotlin
- In Operator (in and !in) in Kotlin
- Is Operator (is and !is) in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Not Null Assertion Operator in Kotlin
- Safe Call Operator (?.) in Kotlin
- Elvis Operator (?:) in Kotlin
- Range Operator (..) in Kotlin
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- print and println Data Output in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin