1. Declaring variables
Let’s once again recall how to create variables. In Kotlin, a variable is declared like this: var name: Type or val name: Type.
- var (short for variable) is a variable that can be assigned a new value (mutable).
- val (short for value) is 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 in the same code block. But you can in different ones. 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 I’ll remind you again 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 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 |
|---|---|
|
|
|
|
|
|
|
|
|
|
This is much more compact and clearer.
Now that we’ve already figured out how to create variables, let’s get acquainted with the 5 most commonly used types in Kotlin. These are: Int (integers), String (text/strings), Double (floating-point numbers), Char (characters), and Boolean (boolean type).
2. Type Int
In a variable of type Int, you 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 set some text string, in Kotlin you need to write the text of the string 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, then here’s another interesting fact.
Strings in Kotlin 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. This is also simple: 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—code uses 3.14, not 3,14.
Here is what floating-point numbers will 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 the start it seems boring, but it’s exactly the building block from which checks and program logic are built later. 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 the profile:
fun main() {
val isStudent = true
val likesPineappleOnPizza = false
println(isStudent) // true
println(likesPineappleOnPizza) // false
}
Yes, this looks a bit philosophical: “do you like pineapple on pizza—false.” But it demonstrates perfectly 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 is 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 you can see the notation. Usually Kotlin can infer it anyway, but explicitness is sometimes 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 quickly starts guessing the type from the way the value looks. This is a useful skill: you get less confused and find errors 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 convenient to keep the following table 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 on it that people most often “trip up” on day one: they forgot quotes, mixed up quotes, put a comma instead of a dot, wrote 'AB' and were surprised why it’s not a 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?
Actually, it’s simple. To print something to the screen, we use the println() function, where we pass what we want to print as an argument.
fun main() {
println("Amigo") // Amigo
println("Ami" + "go") // Amigo
val s1: String = "Amigo"
println(s1) // Amigo
val s2: String = "Am"
println(s2 + "igo") // Amigo
}
I hope things have become 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 check whether you’ve truly figured everything out.
GO TO FULL VERSION