1. Overview

As we’ve known, we can declare and assign multiple variables in one single line in Java.

In this tutorial, we’ll explore how to do the same in Kotlin.

2. Introduction to the Problem

Like Java, we can declare and assign multiple variables in one line. In this tutorial, we’ll addresses approaches to do that:

For simplicity, we’ll use unit tests to verify if the variables have expected values after the assignments. So next, let’s first create three variables holding expected values:

private val expectedString = "Kotlin is awesome!"
private val expectedLong = 42L
private val expectedList = listOf("I", "am", "a", "list", ".")

Next, let’s see the approaches in action.

3. Separating Declarations by Semicolons

In Java, we can declare and assign multiple variables only in the same type at once, such as:

// Java code
String str1 = "Hello World!", str2 = "Hi there", str3 = "See you later";

As the example above shows, we’ve declared three String variables in one single line separated by commas.

In Kotlin, we can achieve it similarly using semicolons as separators. Let’s see a test method:

 val aLong = 42L; val aString = "Kotlin is awesome!"; val aList = listOf("I", "am", "a", "list", ".")
 assertThat(aLong).isEqualTo(expectedLong)
 assertThat(aString).isEqualTo(expectedString)
 assertThat(aList).isEqualTo(expectedList)

If we run the test, it passes. So, the variable assignments work as expected.

Sharp eyes may have noticed that, unlike Java, in Kotlin, we can declare and assign multiple variables of different types in one line.

4. Assigning Two Variables Using a Pair

Alternatively, using a Pair object, we can also assign two variables of different types at once:

val/var (var1:Type1, var2:Type2) = Pair(value1,value2) 
or
val/var (var1:Type1, var2:Type2) = value1 to value2

Let’s understand it quickly through a test method:

val (aLong, aString) = 42L to "Kotlin is awesome!"
assertThat(aLong).isEqualTo(expectedLong)
assertThat(aString).isEqualTo(expectedString)

In the example above, we don’t explicitly declare the types (Long and String) for the two variables. This is because Kotlin is so intelligent that it can infer the type from the value (42L -> Long, and “Kotlin is …” -> String).

However, what if we want to assign more than two variables at once? Next, let’s figure it out.

5. Assigning Multiple Variables Using an Array or a List

First, let’s see an example to assign three variables of different types using an Array object:

val (aLong, aString, aList) = arrayOf(42L, "Kotlin is awesome!", listOf("I", "am", "a", "list", "."))
assertThat(aLong).isEqualTo(expectedLong)
assertThat(aString).isEqualTo(expectedString)
assertThat(aList).isEqualTo(expectedList)

As we can see in the example above, the syntax is pretty similar to the Pair approach. The differences are that we’ve created three variables on the left hand and it’s an Array object on the right hand.

Similarly, the assignment works as well if we replace the array object with a List instance:

val (aLong, aString, aList) = listOf(42L, "Kotlin is awesome!", listOf("I", "am", "a", "list", "."))
assertThat(aLong).isEqualTo(expectedLong)
assertThat(aString).isEqualTo(expectedString)
assertThat(aList).isEqualTo(expectedList)

It’s worth mentioning that when we assign multiple variables using an array or a list, we should make sure the order of variables on the left side coincides with the order of the elements in the array or list.

6. Using Destructuring Declarations

Finally, let’s have a look at how Kotlin’s destructuring declarations declare and assign multiple variables. Before we start, let’s create a pretty simple data class, Article:

data class Article(val title: String, val author: String, val words: Long, val published: Boolean)

As the class above shows, the Article class has four properties. In turn, they are two Strings (title and author), one Long (words), and one Boolean (published).

Kotlin’s destructuring declaration allows us to destructure an object into a number of variables. In other words, if we have an Article object, we can destructure the object into two String variables, one Long variable, and one Boolean variable. Let’s see an example:

val anArticle = Article("Define multiple variables at once in Kotlin", "Kai", 4200L, false)
val (title, author, noOfWords, publishedAlready) = anArticle
assertThat(title).isEqualTo(anArticle.title)
assertThat(author).isEqualTo(anArticle.author)
assertThat(noOfWords).isEqualTo(anArticle.words)
assertThat(publishedAlready).isFalse

Here, we should note that the order of the variables declared on the left side, val (title, author, noOfWords, publishedAlready), should have the same order as that of the declared properties in the Article class.

In our example, the Article class has four properties. However, we may not need all of them to be destructured into variables. For example, we may only want to save an Article‘s title: String and published: Boolean in two variables. So, if we don’t need a variable in the destructuring declaration, we can place an underscore on the left side declaration:

val anArticle = Article("Define multiple variables at once in Kotlin", "Kai", 4200L, false)
val (title, _, _, publishedAlready) = anArticle
assertThat(title).isEqualTo(anArticle.title)
assertThat(publishedAlready).isFalse

7. Conclusion

In this article, we’ve learned the Kotlin way of declaring and assigning multiple variables in one line. We can do it in a few approaches, such as semicolon-separated declarations, by a List, using destructuring declarations, and so on.

The main difference compared to Java is that, in Kotlin, we can declare and assign multiple variables of different types in one line.

As always, the full source code used in the article can be found over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.