CodeGym /Courses /Kotlin SELF /Conversion between data types

Conversion between data types

Kotlin SELF
Level 2, Lesson 3
Available

1. Why do we need type conversion?

Kotlin is a strongly (statically) typed language. If a computer has a “little box” for a number, then trying to stuff text in there is roughly the same as trying to plug a fork into a USB port. The compiler will not forgive you for that! 😡

However, in real life we often work with different data types at the same time. For example, you have a string that contains a number. That is where conversion (or transformation) of types is needed.

Let’s look at an example. Suppose you want to store the number of users:

val a: Int = 1
val b: Int = "2"
val c: Int = "three"

The compiler will immediately say: “Who do you think I am? I can’t automatically convert a string to a number; I can’t do it without you. You need to explicitly convert the string to a number.”

In Kotlin there are several ways to do conversions, and each has its own pros, cons, and typical pitfalls.

2. Converting Int to String

In our work, we often need to get a string representation of a number: for example, to display it on the screen, save it to a file, send it over the network, concatenate it with text, etc. In Kotlin there are several ways to do this, and each is convenient in different situations.

The toString() function

This is the main and most common way:

val number = 42
val str = number.toString()  // str == "42"

toString() converts a value into a string that corresponds to the type of that value.

Concatenation with an empty string

An old, but working way:

val number = 42
val str = "" + number

This approach works quickly for simple cases, but it is less explicit for the programmer reading the code.

3. Implicit conversion to a string

As mentioned above, the Kotlin developers made it so that absolutely all variables, objects, and expressions in Kotlin can be converted to the String type.

Moreover, this happens automatically when we add a String to some other type. Examples:

val a = 5
val name = "Anya" + a            // name contains the string Anya5
val a = 5
val city = a + "New York" + a   // city contains the string 5New York5
val number = 10
val code = "Yo"
val message = "Hello! " + number + code // message contains the string Hello! 10Yo

In all three examples, we calmly added variables of types Int and String, and the result was always of type String.

Important! You cannot perform arithmetic operations with the String type. Even if the string consists entirely of digits.

Examples:

val a = 5
val name = "1" + a              // name contains the string 15
val a = 5
val city = a + "9" + a          // city contains the string 595
val number = 10
val code = "10"
val message = "" + number + code // message contains the string 1010

The addition operation is performed left to right, so the result can be somewhat unexpected. Example:

val a = 5
val name = a + a + "1" + a      // name contains the string 1015

Order of evaluation: ((a + a) + "1") + a

4. Converting a string to a number: toInt()

To convert a number to a string in Kotlin, it is enough to add it to an empty string:

val str = "" + number

But what if you need to convert a string to a number? Well, you can’t convert any string to a number. But if the string consists only of digits, then you still can. For this, Kotlin has a special function.

This command looks like this:

val x: Int = string.toInt()
Converting a string to a number via toInt()

Where val x: Int is the declaration of an integer variable x, and string is a number given as a string (a string consisting of digits).

Examples:

val str = "123"
val number1 = str.toInt()               // number1 contains the number 123

val number2 = "321".toInt()             // number2 contains the number 321

val number3 = ("321" + 0).toInt()       // number3 contains the number 3210

val number4: Int = "321"                // Will not compile: variable is of type Int, but the value is of type String

This works for negative values as well: the toInt() function is very smart.

5. toIntOrNull(): safe conversion to a number

When a string comes from the outside world (from a file or from the user), there is always a risk that it is not a number. And this is where a safer method appears: toIntOrNull().

It works like this: if the string really contains an integer, it will return an Int. If not, it will return null. And this is fundamentally different behavior compared to toInt(): instead of an error at runtime and a program crash, we get a special value — null.

null is a special value that means “there is no value.” That is, null is not 0, not an empty string, and not an “error as text.” It is exactly “no result.”

Extending the Int type: Int?

The Int type stores only valid integers; it cannot store strings, fractional numbers, and it also cannot store null. That is why Kotlin has an extended variant of the Int type — Int?. It stores all values of type Int and the value null (no value).

This situation is so common that in Kotlin it was made part of the language: you can add a question mark to any type in Kotlin and get an “extended type” that can also store null.

If a normal Int always stores a number, then Int? is read as: “either Int, or null.” The question mark is literally a marker meaning “may be absent.”

The toIntOrNull() method returns Int? because it can both successfully parse a number and fail to parse it.

Mini example: successful and unsuccessful parsing

Right now, it is very useful to see what happens in both cases, and not try to “guess.”

fun main() {
    val ok: Int? = "15".toIntOrNull()
    val bad: Int? = "15a".toIntOrNull()

    println(ok)   // 15
    println(bad)  // null
}

6. Cheat sheet and conversion practice

When there are several methods, the brain asks for a “cheat sheet.” That’s normal: you are not required to keep everything in your head — you are required to be able to quickly reconstruct the meaning.

Quick reference table: toString() vs toInt() vs toIntOrNull()

Method From → to When to use What happens if the string is not a number
toString()
Int → String
need to display/concatenate text not applicable (we are converting from a number)
toInt()
String → Int
you are sure the string is valid runtime error (exception)
toIntOrNull()
String → Int?
the string may be “messy” returns null

Why Kotlin forces you to be explicit

When people first encounter explicit conversions, a desire arises: “Why doesn’t the language itself figure out that "123" is a number?” But the language explicitly does not want to guess.

Imagine you wrote "007". Is that the number seven? Or is it a department code? Or is it a string where leading zeros matter? If the language starts automatically turning strings into numbers, it will constantly guess wrong. Kotlin chooses predictability: if you want a number, write toInt() or toIntOrNull() and take responsibility yourself.

And that is actually a very mature stance for a language. Kotlin is like saying: “I’m not a telepath. I’m a compiler.”

1
Task
Kotlin SELF, level 2, lesson 3
Locked
Score label
Score label
1
Task
Kotlin SELF, level 2, lesson 3
Locked
Concatenation vs. Sum
Concatenation vs. Sum
1
Task
Kotlin SELF, level 2, lesson 3
Locked
Form review
Form review
1
Task
Kotlin SELF, level 2, lesson 3
Locked
Year round-trip
Year round-trip
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION