- 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
- Java Web開發學習手冊
- 機器人Python青少年編程開發實例
- Python編程完全入門教程
- Monitoring Elasticsearch
- Mastering JavaScript Design Patterns(Second Edition)
- Mastering ArcGIS Enterprise Administration
- Android傳感器開發與智能設備案例實戰
- Android Studio開發實戰:從零基礎到App上線 (移動開發叢書)
- Visual Basic程序設計實驗指導及考試指南
- IBM RUP參考與認證指南
- Java 7 Concurrency Cookbook
- Linux Networking Cookbook
- 大象:Thinking in UML(第二版)
- Python Natural Language Processing
- Unity AI Game Programming(Second Edition)