'Define multiple variables at once in Kotlin (e.g Java : String x,y,z;)
I was wondering if there is any way to define multiple variables in Kotlin at once like in Java and almost every other existing language in the world .
like in Java :
String x = "Hello World!", y = null, z;
Solution 1:[1]
Try this:
fun main() {
val (x, y, z) = listOf(1, true, "Sam") //can be "arrayOf(), "Pair()" or other types
println("$x, $y, $z")
}
Output?
1, true, Sam
Solution 2:[2]
You might also find "Destructuring declarations?" helpful here.
An example:
val (name, age) = person
More detail at https://kotlinlang.org/docs/destructuring-declarations.html.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Qaz |
| Solution 2 | Bink |
