0
0
Kotlinprogramming~15 mins

Any type as universal base in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Any Type as a Universal Base in Kotlin
📖 Scenario: You are building a simple Kotlin program that stores different types of items in a list. Since the items are of different types, you will use Kotlin's Any type, which can hold any kind of value.
🎯 Goal: Create a Kotlin program that uses the Any type as a universal base to store and print different types of values.
📋 What You'll Learn
Create a list called items that can hold values of any type
Add at least three different types of values to the items list
Create a variable called count to store the number of items
Use a for loop to print each item in the items list
💡 Why This Matters
🌍 Real World
In real apps, you often need to store or handle data of different types together. Kotlin's <code>Any</code> type lets you do this safely and easily.
💼 Career
Understanding <code>Any</code> helps you work with generic data, APIs, and collections that hold mixed types, which is common in software development.
Progress0 / 4 steps
1
Create a list with different types using Any
Create a list called items that holds these exact values: 42 (an integer), "Hello" (a string), and true (a boolean). Use the Any type for the list.
Kotlin
Need a hint?

Use val items: List<Any> = listOf(42, "Hello", true) to create the list.

2
Add a variable to count the number of items
Create a variable called count and set it to the size of the items list.
Kotlin
Need a hint?

Use val count = items.size to get the number of items.

3
Use a for loop to print each item
Use a for loop with the variable item to go through items and print each item.
Kotlin
Need a hint?

Use for (item in items) { println(item) } to print each item.

4
Print the total count of items
Print the text "Total items: " followed by the value of count using a single println statement.
Kotlin
Need a hint?

Use println("Total items: $count") to print the count.