0
0
Kotlinprogramming~15 mins

Type checking with is operator in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Type Checking with is Operator in Kotlin
📖 Scenario: Imagine you are building a simple program that handles different types of inputs. Sometimes the input is a number, and sometimes it is text. You want to check the type of the input to decide what to do next.
🎯 Goal: You will create a Kotlin program that uses the is operator to check the type of a variable and print a message based on whether it is an Int or a String.
📋 What You'll Learn
Create a variable named input with a specific value.
Create a variable named message to hold the result message.
Use the is operator to check if input is an Int or a String.
Assign the correct message to message based on the type check.
Print the message.
💡 Why This Matters
🌍 Real World
Type checking is useful when you receive data that can be different types, like user input or data from the internet. It helps your program decide what to do next safely.
💼 Career
Understanding type checking is important for writing safe and bug-free Kotlin programs, especially in Android app development and backend services.
Progress0 / 4 steps
1
Create the input variable
Create a variable called input and set it to the integer value 42.
Kotlin
Need a hint?

Use val input: Any = 42 to create the variable.

2
Create the message variable
Create a variable called message and set it to an empty string "".
Kotlin
Need a hint?

Use var message: String = "" to create the variable.

3
Use the is operator to check the type
Use an if statement with the is operator to check if input is an Int. If yes, set message to "Input is an integer". Otherwise, check if input is a String and set message to "Input is a string".
Kotlin
Need a hint?

Use if (input is Int) and else if (input is String) to check types.

4
Print the message
Write a println statement to print the value of message.
Kotlin
Need a hint?

Use println(message) to show the message.