Using <code>var</code> for Mutable References in Kotlin
📖 Scenario: Imagine you are managing a simple inventory system for a small store. You want to keep track of the number of items available and update this number as items are sold or restocked.
🎯 Goal: You will create a mutable variable using var in Kotlin to hold the inventory count. Then, you will update this count to reflect sales and restocking.
📋 What You'll Learn
Create a mutable variable called
inventoryCount with an initial value of 50.Create a variable called
soldItems with the value 5.Update
inventoryCount by subtracting soldItems.Print the updated
inventoryCount.Create a variable called
restockedItems with the value 10.Update
inventoryCount by adding restockedItems.Print the final
inventoryCount.💡 Why This Matters
🌍 Real World
Managing inventory counts is a common task in stores, warehouses, and online shops. Using mutable variables helps keep track of changing stock levels.
💼 Career
Understanding mutable variables is essential for any Kotlin developer working on apps that handle data changes, such as inventory systems, games, or user settings.
Progress0 / 4 steps