Variables store values so your program can remember and reuse information. In Android apps, variables hold things like usernames, loading states, API responses, selected tabs, and form input.
Kotlin has two main ways to declare a variable:
val appName = "Tasky"
var taskCount = 3
Use val when the value should not be reassigned. Use var only when the value needs to change. Prefer val by default because it makes code easier to reason about.
Common Types
Kotlin can infer types, but knowing them helps you read code clearly.
val title: String = "Learn Kotlin"
val lessonsCompleted: Int = 4
val rating: Double = 4.8
val isLoggedIn: Boolean = true
In Android, these types often map directly to UI state:
val buttonText = "Continue"
val progress = 75
val showError = false
Operators
Operators let you calculate, compare, and combine values.
val total = 10 + 5
val remaining = total - 3
val isComplete = remaining == 0
val canProceed = isComplete || remaining < 5
Important operators:
| Operator | Meaning |
|---|---|
+, -, *, / | math |
==, != | equality |
>, <, >=, <= | comparison |
&& | both conditions must be true |
| ` | |
! | invert a boolean |
Nullability
Kotlin separates values that can be missing from values that cannot.
val name: String = "Akshay"
val nickname: String? = null
println(nickname?.uppercase() ?: "No nickname")
Use nullable types only when absence is a valid state.
Practice
Create variables for a course name, total lessons, completed lessons, and whether the user is premium. Calculate the remaining lessons and print a message like You have 6 lessons left.
Summary
Variables hold data, types describe what kind of data is allowed, and operators let you work with that data. Good Android code starts with clear, predictable state.