1. Declaring variables
Let’s remember once again how to create variables. In Kotlin, a variable is declared like this: var name: Type or val name: Type.
- var (short for variable) — a variable that can be assigned a new value (mutable).
- val (short for value) — a variable that can be assigned a value only once (immutable).
Examples:
| Command | Explanation |
|---|---|
|
A variable of type is created. Such a variable can store text. |
|
A variable of type is created. Such a variable can store integers. |
|
A variable of type is created. Such a variable can store fractional numbers. |
You can’t create two variables with the same names within one code block. But in different blocks you can. It’s like boxes standing in different houses. More about code blocks — in the next lectures.
There are also restrictions on the variable name. On the one hand, it can be anything; on the other hand, it can’t contain spaces, the symbols +, -, etc. It’s best to use only Latin letters and digits in a variable name.
And again, I remind you that in the Kotlin language it matters which letters you write — uppercase or lowercase. var a: Int is not the same as var a: int (in Kotlin there is no type int at all — there is Int).
2. Type inference
Kotlin is very smart and tries to make your life as easy as possible. If you create a variable and immediately assign it a value, then Kotlin can determine the variable’s type based on its value.
In other words, you don’t have to write the variable type if it can be unambiguously determined from the value assigned to the variable. This saves time and space. Examples:
| Compact code | Long code, equivalent to the code on the left |
|---|---|
|
|
|
|
|
|
|
|
|
|
That’s much more compact and clear.
Now that we’ve already figured out how to create variables, let’s get to know the 5 most commonly used types in the Kotlin language. These are the types Int (integers), String (text/strings), Double (floating-point numbers), Char (characters), and Boolean (logical type).
2. Type Int
A variable of type Int can store integers: 5, -10, 100500. You can also perform various arithmetic operations on the Int type: addition, subtraction, multiplication, division, and others. Examples:
val x: Int = 1 // x will be equal to 1
val y: Int = x * 2 // y will be equal to 2
val z: Int = 5 * y * y + 2 * y + 3 // z will be equal to 20+4+3, will be equal to 27
val a: Int = 5 // a will be equal to 5
val b: Int = 1 // b will be equal to 1
val c: Int = (a - b) * (a + b) // c will be equal to 4*6, will be equal to 24
val a: Int = 64 // a will be equal to 64
val b: Int = a / 8 // b will be equal to 8
val c: Int = b / 4 // c will be equal to 2
val d: Int = c * 3 // d will be equal to 6
If you didn’t skip math in school, everything here should be clear.
3. Type String
The String type allows you to store text strings. And to specify a text string in Kotlin, you need to write the string text and put double quotes on both sides. Example:
| Code | Explanation |
|---|---|
|
will contain the text |
|
will contain the text |
|
will contain the text |
Doesn’t look hard, right? If so, here’s one more interesting fact.
In Kotlin, strings can be concatenated using the plus sign — +. Example:
fun main() {
val s1: String = "Amigo" + " the best" // s1 will contain Amigo the best
val s2: String = "" // s2 will contain an empty string — no characters at all.
val x: Int = 2025
val s3: String = "Amigo" + x // s3 will contain Amigo2025
}
Pay attention to the last example: we added a string and a number. It’s also simple here: the number will be converted to a string, then the two strings will be concatenated. When adding strings and numbers, the result is always a string.
4. Double — numbers with a fractional part
Double is needed when there is a fractional part: price, temperature, GPA, coordinates, percentages. It’s easy to recognize a Double at a glance: the literal contains a dot. Yes, a dot — in code you write 3.14, not 3,14.
Here’s what floating-point numbers look like in code:
fun main() {
val heightMeters = 1.78
val coffeeRating = 4.5
println(heightMeters) // 1.78
println(coffeeRating) // 4.5
}
It’s important to grasp the idea: 1.78 is not “almost an integer.” It’s a value of a different type, and Kotlin will treat it as a floating-point number.
5. Boolean — true/false
Boolean is a type that stores only two values: true or false. At first it seems boring, but it’s exactly the building block from which checks and program behavior logic are later built. For now, it’s important to simply understand: true/false are not strings (there are no quotes) and not numbers.
Let’s add a couple of “flags” to a profile:
fun main() {
val isStudent = true
val likesPineappleOnPizza = false
println(isStudent) // true
println(likesPineappleOnPizza) // false
}
Yes, this looks slightly philosophical: “you like pineapple on pizza — false.” But it perfectly demonstrates that a variable can store not only text and numbers.
6. Char — a single character
Char is one single character. It is written in single quotes: 'A', '7', '?'. This is not a string. A string can be 1 character long ("A"), but it’s still a different type.
Kotlin explicitly emphasizes: characters are represented by the Char type, and Char literals are written in single quotes.
Here’s what it looks like in code:
fun main() {
val firstLetter: Char = 'A'
val separator: Char = '#'
println(firstLetter) // A
println(separator) // #
}
Here I intentionally specified the type explicitly (: Char) so that you can see the notation. Usually Kotlin will figure it out anyway, but sometimes being explicit is useful — especially when you’re just learning to read code.
7. Literals: how to tell the type from how a value is written
When you look at code, your brain very quickly starts guessing the type from the value’s appearance. This is a useful skill: you get less confused and find mistakes faster. Kotlin does roughly the same thing: it analyzes the literal and decides which type to treat it as (or asks you to clarify).
It’s handy to keep a table like this in mind:
| How the value is written in code | Example | What type it is |
|---|---|---|
| Digits without quotes and without a dot | |
|
| There is a dot in the number | |
|
| Double quotes | |
|
/ |
|
|
| A single character in single quotes | |
|
This table seems obvious, but it’s exactly here that people most often “stumble” on day one: they forget quotes, mix up quotes, put a comma instead of a dot, write 'AB' and then wonder why it’s not Char.
8. Printing a variable to the screen
Everything seems so obvious and elementary. Then maybe you can immediately guess which command can print a variable to the screen?
In fact, it’s simple. To print something to the screen, we use the println() function, and as its parameter we pass what we want to print.
fun main() {
println("Amigo") // Amigo
println("Ami" + "go") // Amigo
val s1: String = "Amigo"
println(s1) // Amigo
val s2: String = "Am"
println(s2 + "igo") // Amigo
}
Hopefully it’s a bit clearer now. And whether you understood everything correctly or not, we’ll check right now. Practice is the criterion of truth: only with practice can you verify that you’ve really figured everything out.