- Android Development with Kotlin
- Marcin Moskala Igor Wojda
- 543字
- 2021-07-02 18:48:33
Variables
In Kotlin, we have two types of variables: var and val. The first one, var, is a mutable reference (read-write) that can be updated after initialization. The var keyword is used to define a variable in Kotlin. It is equivalent to a normal (non-final) Java variable. If our variable needs to change at some point, we should declare it using the var keyword. Let's look at an example of a variable declaration:
fun main(args: Array<String>) { var fruit: String = "orange" //1 fruit = "banana" //2 }
- Create a fruit variable and initialize it with the vale of the orange variable.
- Reinitialize the fruit variable with the value of the banana variable.
The second type of variable is a read-only reference. This type of variable cannot be reassigned after initialization.
val random: Int
get() = Random().nextInt()
Custom getters will be discussed in more detail in Chapter 4, Classes and Objects.
The val keyword is the equivalent of a Java variable with the final modifier. Using immutable variables is useful, because it makes sure that the variable will never be updated by mistake. The concept of immutability is also helpful for working with multiple threads without worrying about proper data synchronization. To declare immutable variables, we will use the val keyword:
fun main(args: Array<String>) { val fruit: String= "orange"http://1 fruit = "banana" //2 Error }
- Create the fruit variable and initialize it with the value of the orange string.
- The compiler will throw an error, because the fruit variable was already initialized.
Notice that the type of the variable reference (var, val) relates to the reference itself, not the properties of the referenced object. This means that when using a read-only reference (val), we will not be able to change the reference that is pointing to a particular object instance (we will not be able to reassign variable values), but we will still be able to modify properties of referenced objects. Let's see it in action using an array:
val list = mutableListOf("a","b","c") //1 list = mutableListOf("d", "e") //2 Error list.remove("a") //3
- Initialize a mutable list.
- The compiler will throw an error, because a value reference cannot be changed (reassigned).
- The compiler will allow us to modify the content of the list.
The val keyword cannot guarantee that the underlying object is immutable.
If we really want to make sure that the object will not be modified, we must use an immutable reference and an immutable object. Fortunately, Kotlin's standard library contains an immutable equivalent of any collection interface (List versus MutableList, Map versus MutableMap, and so on) and the same is true for helper functions that are used to create instances of a particular collection:
Variable/value definition
Reference can change
Object state can change
val = listOf(1,2,3)
No
No
val = mutableListOf(1,2,3)
No
Yes
var = listOf(1,2,3)
Yes
No
var = mutableListOf(1,2,3)
Yes
Yes
- Learning NServiceBus(Second Edition)
- Android開發精要
- Vue.js入門與商城開發實戰
- Bulma必知必會
- AngularJS深度剖析與最佳實踐
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第2版)
- HTML5+CSS3+JavaScript Web開發案例教程(在線實訓版)
- C語言程序設計
- Mathematica Data Analysis
- Gradle for Android
- Getting Started with Gulp
- 零基礎學C語言第2版
- Spring技術內幕:深入解析Spring架構與設計原理(第2版)
- 零基礎學HTML+CSS第2版
- Unity 5.X從入門到精通