val vs var: Immutability From Day One
Understand the difference between val and var and why immutability matters.
val vs var: Immutability From Day One is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
Two Ways to Declare a Variable
val: Read-Only Reference
val name = "Kotlin"
println(name)
// name = "Java" // Error: Val cannot be reassignedvar: Mutable Reference
var counter = 0
counter = 1
counter += 1
println(counter) // 2Type Inference
val age = 25 // inferred: Int
val name: String = "Ana" // explicit type
var score: Double = 0.0val Does Not Mean Immutable Object
val list = mutableListOf(1, 2, 3)
list.add(4) // OK! list is val but mutable
// list = mutableListOf() // Error: reassignmentPrefer val by Default
val pi = 3.14159 // constant value
val greeting = "Hi" // never changes
var retries = 3 // will be decrementedval vs const val
const val MAX_SIZE = 100 // compile-time
val instance = MyClass() // runtimeUninitialized var
var result: String
result = "computed"
println(result)Scope and Shadowing
val x = 10
run {
val x = 20 // shadows outer x
println(x) // 20
}
println(x) // 10Naming Conventions
val firstName = "Jane" // camelCase
const val MAX_ITEMS = 50 // UPPER_SNAKE_CASE
class UserProfile // PascalCaseWhy Immutability Matters
Quick Check
Recap
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “val vs var: Immutability From Day One” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “val vs var: Immutability From Day One”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “val vs var: Immutability From Day One”?
Understand the difference between val and var and why immutability matters. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “val vs var: Immutability From Day One” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Setting Up Kotlin: REPL, IntelliJ & Online Playground
- val vs var: Immutability From Day One
- Kotlin Basic Types: Int, Double, Boolean, Char
- Your First Kotlin Program: main, println & Input