Kotlin Variables


Kotlin Variables

In Kotlin, variables are like labeled boxes where you can store information — such as numbers, text, or other data — to use later in your program.


Two Main Types of Variables

Kotlin gives you two keywords to declare variables, depending on whether the value can change or not:

1. val – Fixed Value (Read-Only)

  • Use val when you don’t want the value to be modified later.
  • Think of it like a constant label – once it's set, it's locked.
val name = "Alice" 
The value "Alice" cannot be changed after this line.

2. var – Changeable Value

  • Use var when the variable might change in the future.
  • It gives you flexibility to update the value.
var age = 25 
age = 26 

Kotlin is Smart – Type Inference

You don’t always need to tell Kotlin what kind of data you're storing — it can figure it out automatically. kotlin Copy Edit

val city = "Paris"       
var temperature = 21.5  

But if you want to be specific, you can mention the type:

val isOnline: Boolean = true
Var score: Int = 90 

Tip: Always Prefer val When Possible

Using val makes your code safer and easier to read, because it protects your values from accidental changes.


Summary

Keyword Can Change? Good For
val No Constants, fixed data
var Yes Dynamic values

Prefer Learning by Watching?

Watch these YouTube tutorials to understand KOTLIN Tutorial visually:

What You'll Learn:
  • 📌 #5 Kotlin Tutorial | Var Val
  • 📌 Learning Kotlin: Variables
Previous Next