Hello! Welcome to your first lesson in our course on Kotlin for Android Development. I'm excited to get started with you.
Given your background in Java and your goal to build modern Android apps, we'll start by diving into the fundamental building blocks of the Kotlin language, always drawing parallels to what you already know to make the transition as smooth as possible.
This first lesson focuses on a core concept you'll use constantly: how to declare variables. We will cover the two keywords Kotlin uses for this, val and var, and explore the crucial difference between them—the concepts of immutability and mutability.
By the end of this lesson, you will be able to declare variables in Kotlin and confidently choose the right tool for the job based on whether the variable's value needs to change.
From Java's Variables to Kotlin's val and var
In Java, you declare variables by specifying their type and name, like String name = "John";. If you want a variable that cannot be reassigned, you add the final keyword: final String USER_ID = "12345";.
Kotlin simplifies this and makes the distinction between re-assignable and non-re-assignable variables more explicit and central to the language design. Let's see how.
To start, let's watch a short video that provides a quick and clear introduction to variable declaration in Kotlin.
Kotlin Crash Course for Java Developers
This video from Coding Master gives a concise overview of how to declare variables using val for immutable (read-only) references and var for mutable (changeable) ones.
Watch the section from 04:23 to 06:22. Pay attention to the syntax and the compiler error shown when trying to reassign a val.
As you saw, Kotlin uses two distinct keywords:
val(from "value"): Used for variables whose reference is immutable. Once assigned, they cannot be reassigned.var(from "variable"): Used for variables whose reference is mutable. They can be reassigned to a new value.
Understanding Mutability and Immutability
This distinction is one of the most important concepts in modern programming. Using immutable data by default leads to code that is safer and easier to reason about, especially in complex applications.
To get a deeper understanding, please read through the official Android developer documentation. It breaks down the syntax and, most importantly, explains the difference between read-only (val) and mutable (var) variables with clear examples.
Create and use variables in Kotlin
This codelab from the official Android Developers site provides a detailed explanation of variables in Kotlin. We'll focus on the section that contrasts val and var.
Please read section 4, 'Update variables'. It starts with a shopping cart example and clearly explains why val causes an error upon reassignment and how to use var to allow updates. Focus on the definitions of 'read-only' and 'mutable'.
The key takeaway from the reading is the principle of preferring val over var. You should always declare a variable with val unless you have a specific reason to change it later. This makes your intentions clear and prevents accidental modifications.
Here is a visual summary that is targeted specifically at developers like you who are coming from a Java background.

Connecting the Dots: Kotlin's val and Java's final
Your experience with Java's final keyword provides a direct parallel here. A Kotlin val is conceptually very similar to a Java final variable.
This next reading provides a direct, side-by-side comparison that should solidify the concept for you.
Kotlin vs Java – A Comprehensive Comparison
This article from Bugfender offers a concise comparison between Kotlin and Java on several features. We'll look at the section on variable declaration.
Read the 'Variable declaration' section. Note how it directly maps Kotlin's val to Java's final and explains var as the standard mutable variable.
Let's look at this in code.
In Java, you would write:
// A re-assignable variable
int score = 0;
score = 10; // OK
// A non-re-assignable (constant) variable
final String userId = "uid-123";
// userId = "uid-456"; // Compile error
In Kotlin, the equivalent code is more concise:
// A mutable (re-assignable) variable
var score: Int = 0
score = 10 // OK
// An immutable (read-only) variable
val userId: String = "uid-123"
// userId = "uid-456" // Compile error: Val cannot be reassigned
Notice that in Kotlin, the choice between immutability and mutability is a mandatory part of every variable declaration via the val/var keyword.
Practical Application
Think about an Android application. When defining the data for a user profile screen, which of the following properties would you declare with val and which with var?
userIddisplayNameprofilePictureUrllastLoginDatefriendCount
Click to see my thoughts
val userId: A user's ID is typically assigned once and never changes. This is a perfect candidate forval.var displayName: A user might be allowed to change their display name.varwould be appropriate here.var profilePictureUrl: Similar to the display name, a user can update their profile picture, so its URL would need to change.varis suitable.var lastLoginDate: This value will be updated every time the user logs in, so it must be avar.var friendCount: As the user adds or removes friends, this count will change. It should be avar.
The key is to ask: "Will this value ever need to change after its initial assignment?" If the answer is no, use val. Otherwise, use var.
Key Takeaways
This lesson covered the absolute basics of variables in Kotlin, a concept you'll apply in every line of code you write.
- Two Keywords: Variables are declared with
val(immutable reference) orvar(mutable reference). - Immutability First:
valcreates a read-only variable, similar tofinalin Java. Once a value is assigned, it cannot be reassigned. - Mutability When Needed:
varcreates a traditional variable whose value can be changed. - Best Practice: Always prefer
valovervar. This makes your code safer, more predictable, and easier to debug.
Next Steps
Now that you understand how to declare variables, our next lesson will explore Kotlin's basic data types (like Int, String, Boolean) and a powerful feature called type inference, which makes your code even more concise.