var & val

When you create an identifier that holds data, you must decide whether it can be reassigned.

Identifiers are the words you create so you can refer to elements in your program. The most basic decision you make about an identifier that refers to data is whether it can change its assignment during program execution, or if it can only be assigned once. This is controlled by two keywords:

You define a var like this:

var identifier = initialization

The var keyword is followed by the identifier, an equals sign and then the initialization value. The identifier begins with a letter or an underscore, followed by letters, numbers and underscores. Upper and lower case are distinguished (so thisvalue and thisValue are different).

Here are some var definitions:

// VarAndVal/Vars.kt fun main(args: Array<String>) { var whole = 11 // [1] var fractional = 1.4 // [2] var words = "A value" // [3] println(whole) println(fractional) println(words) } /* Output: 11 1.4 A value */

In this book we mark lines with commented numbers in square brackets so we can refer to them in the text like this:

Note that println() can take any single value as an argument.

As the name variable implies, a var can vary. That is, you can change the data stored in a var. We say that a var is mutable:

// VarAndVal/AVarIsMutable.kt fun main(args: Array<String>) { var sum = 1 sum = sum + 2 sum += 3 println(sum) } /* Output: 6 */

The assignment sum += 3 means the same as sum = sum + 3. The += operator takes the previous value stored in sum and increases it by 3, then assigns that new result back to sum.

Changing the value stored in a var according to external requests is a useful way to comply with changes in the outside world. However, when the complexity of a program increases, your code is clearer, safer and easier to understand if the values represented by your identifiers cannot change—that is, they cannot be reassigned. We specify an unchanging identifier using the val keyword instead of var. A val can only be assigned once, when it is created:

val name = initialization

The val keyword comes from value which indicates something that cannot change—it is immutable. Choose val instead of var whenever possible. The Vars.kt example at the beginning of this atom can be rewritten using vals:

// VarAndVal/Vals.kt fun main(args: Array<String>) { val whole = 11 // whole = 15 // Error // [1] val fractional = 1.4 val words = "Twas Brillig" println(whole) println(fractional) println(words) } /* Output: 11 1.4 Twas Brillig */

It’s important to choose descriptive names for your identifiers. This makes your code easier to understand and often reduces the need for comments. In Vals.kt, you have no idea what whole represents. If your program is storing the number 11 to represent the time of day when you get coffee, it’s more obvious to others if you name it coffeetime and easier to read if it’s coffeeTime (conventional programming style makes the first letter lowercase).

vars come in handy when data must change as the program is running—which sounds like a common activity, but turns out to be avoidable in practice. That is, you can virtually always use a val instead of a var through some small code rewrites (or often, no rewrites at all).

In general, your programs are easier to extend and maintain if you use vals. However, on rare occasions it’s too complex to solve a problem using only vals. For that reason, Kotlin gives you the flexibility of vars. However, as you spend more time with vals you’ll discover that you virtually never need vars and that your programs are safer and more reliable without them.

Previous          Next

©2018 Mindview LLC. All Rights Reserved.