Kotlin Data Types

Jakob Persons
5 min readJun 21, 2021

What is Kotlin?

Kotlin is a general purpose programming language created by JetBrains, a Czech software company. Kotlin grew in popularity after its release because of the concise syntax and compatibility with Java. Since Kotlin is a general purpose language, it can be used for Android development, web development and data science. In 2019, Google announced Kotlin as their preferred language for Android development. Some other companies that use Kotlin are Netflix, Pinterest and Duolingo.

Variable Declaration

Kotlin has two different types of variable declaration. The var or val keywords represent mutable or immutable variables respectively. Mutable variables can change value throughout a program, while immutable variables cannot change values throughout a program.

Number Variables

There are various number variables for use within Kotlin. Whole numbers are represented by either Byte, Short, Int or Long data types. Kotlin documentation provides a table that shows the details about each data type for whole numbers.

Kotlin whole number data types

Kotlin also has two different floating point number data types. Floating point numbers can either be Floats or Doubles. Again, Kotlin provides tables for the details of each data type within their documentation.

Kotlin floating point number data types

Text Variables

Like numbers, Kotlin has a few different data types for text. The first would be a Char, or character. The char syntax within Kotlin is to surround the character with single quote like so 'a'. It’s important to note here that unlike other languages, like JavaScript or Ruby, single quotes are used to surround a character and double quotes are used to surround a string. They are not interchangeable. Strings are collections of characters surrounded by “double quotes”. They can represent words or sentences.

Chars and Strings can include more than just letters. Numbers, symbols and spaces can also be included within a string or char, as long as they are surrounded with the single quotes or the double quotes.

Within Kotlin, strings are considered to be immutable. This means that whether or not the variable that represents the string is declared using var or val, the string itself cannot be changed. When performing operations on strings, a new string object is created while the original copy remains unchanged.

Collection Types

Kotlin offers a few different options when it comes to collections of data. The first is called a list. A list is an ordered collection of elements that can contain duplicate values. Lists can either be declared as immutable or mutable. For example:

immutable list vs mutable list

In the above examples, list1 has read-only operations available on it. If you attempted to remove or add elements from list1, Kotlin would throw an error. However list2 can both be read and have values re-assigned, removed or added to the list.

Sets are similar to list in both syntax and structure. A set is an unordered collection of data which can also be declared as mutable or immutable just like Lists. The main difference between lists and sets is the nature of the programs behavior. For example, if you have a collection of data that requires order to make sense (such as a list of addresses for a mail route), it would make sense to use a List. However, a list of student grades where order does not necessarily matter, would be better suited for a Set. It is up to the developer to make the decision as to which makes more sense.

Maps are another of Kotlin’s data collection type that provide a key:value pair. Again, maps can be declared as either mutable or immutable. Within a map object, keys must be unique within the collection, while values can be duplicated.

The type of data within lists, sets and maps can either be inferred or explicitly stated upon declaration. For example:

inferred data type vs explicit data type declaration

Note the syntax to declare key and value pairs within a map. "key" to "value" is proper syntax, that will return key=value within the console when printed.

The following Kotlin program:

main function within Kotlin program

Will display the following within the console when run:

Notice how the map object is surround by curly brackets

The syntax and output as a result are a little bit different than they are in other languages like JavaScript, Ruby or even Python — even though there are many similarities in terms of constructing and use of these objects. More or less each data type corresponds to a similar data type within other languages, with just a few minor changes. This concise and simple syntax is part of the reason why Kotlin has become so popular since it creation.

I have created a GitHub repository that shows different variable declarations and usage to display information about the Euro 2021 tournament taking place this summer! Feel free to check it out, clone it to your local machine and run it to see how it works.

--

--

Jakob Persons

Software Engineer | Full Stack Developer | Soccer Fanatic