Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe. It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Kotlin Comments & Variables (Lesson 3 PDF)
Overview about the Lesson
In this lesson, you will learn about comments and variables in Kotlin. You will learn how to use comments to document your code and make it more readable. You will also learn how to declare variables and constants in Kotlin.





The Content of the Lesson
The lesson is divided into two parts. In the first part, you will learn about comments in Kotlin. You will learn how to use single-line and multi-line comments to document your code. You will also learn how to use comments to disable code temporarily. In the second part, you will learn how to declare variables and constants in Kotlin. You will learn about data types, type inference, and how to declare variables and constants.
Why This Lesson
This lesson is designed for programmers who are new to Kotlin and want to learn how to use comments and declare variables and constants. It is also suitable for programmers who are familiar with Java and want to learn Kotlin.
Kotlin Comments and Variables: val vs var Explained
This lesson’s PDF introduces comments and variables. Here are the key ideas with examples you can run in the Kotlin Playground.
Comments
// A single-line comment: ignored by the compiler
/*
A multi-line comment
for longer explanations
*/
/**
* A KDoc comment documents functions and classes.
* @param name the person to greet
*/
fun greet(name: String) = println("Hello, $name")Good comments explain why code does something. Clear names usually explain what it does.
val and var
| Keyword | Meaning | Can be reassigned? |
|---|---|---|
| val | Read-only value (like a constant reference) | No |
| var | Mutable variable | Yes |
fun main() {
val birthYear = 1998
var score = 10
score = score + 5 // OK: var can change
// birthYear = 2000 // Error: val cannot be reassigned
println("Born $birthYear, score $score") // Born 1998, score 15
}Rule of thumb: use val by default and switch to var only when the value really needs to change. Code with fewer changing values is easier to understand and has fewer bugs.
Type inference and explicit types
val city = "Cairo" // Kotlin infers String
val count: Int = 5 // explicit type
var price: Double = 19.99
var nickname: String? = null // ? means the variable may hold nullKotlin works out the type from the value, so you rarely need to write it. Add it when it makes the code clearer or when you declare a variable without a value yet.
Naming rules and conventions
- Names can contain letters, digits and underscores, but cannot start with a digit.
- Use camelCase for variables:
totalPrice,userName. - Names are case-sensitive:
totalandTotalare different. - Keywords such as
val,funorclasscannot be used as names. - For true compile-time constants, use
const val MAX_USERS = 100at the top level, written in UPPER_SNAKE_CASE.
Common mistakes
- Reassigning a
val: the error “Val cannot be reassigned” means you should either usevaror create a new value. - Changing the type:
var x = 5thenx = "five"fails, because x is an Int. - Assigning
nullto a non-nullable type: add?to the type if null is allowed.
Practice exercise
Create a read-only variable for a product name, a mutable variable for stock, reduce the stock by 2 and print the result.
fun main() {
val product = "Keyboard"
var stock = 10
stock -= 2
println("$product stock: $stock") // Keyboard stock: 8
}Frequently Asked Questions
Is val the same as final in Java?
Yes, a val behaves like a final variable: it can be assigned once. The object it points to can still be mutable (for example a MutableList).
When should I use const val?
For values known at compile time that never change, such as configuration limits or keys, declared at the top level or inside an object.
Continue the course
← Previous: Lesson 2: Syntax & Output | Next: Lesson 4: Data Types →
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output
- Lesson 3: Comments & Variables (you are here)
- Lesson 4: Data Types
- Lesson 5: Operators
- Lesson 6: Set Operations & Grouping
- Lesson 7: Booleans & If…Else
- Lesson 8: When, While, Break & Continue
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges
- Lesson 11: Functions & Classes
- Lesson 12: Classes, Objects & Constructors
- Lesson 13: Class Functions & Inheritance
Conclusion
Comments and variables are essential parts of any programming language. In this lesson, you learned how to use comments to document your code and make it more readable. You also learned how to declare variables and constants in Kotlin. With these skills, you can write more efficient and maintainable code.
Download From Link
You can download the lesson from the following link:














