- 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
- Python神經(jīng)網(wǎng)絡(luò)項目實戰(zhàn)
- Windows Server 2012 Unified Remote Access Planning and Deployment
- Unity Shader入門精要
- STM32F0實戰(zhàn):基于HAL庫開發(fā)
- Python機器學(xué)習(xí)算法: 原理、實現(xiàn)與案例
- Serverless Web Applications with React and Firebase
- 一步一步跟我學(xué)Scratch3.0案例
- 小程序從0到1:微信全棧工程師一本通
- Emotional Intelligence for IT Professionals
- Learning Android Application Testing
- 深入實踐DDD:以DSL驅(qū)動復(fù)雜軟件開發(fā)
- AMP:Building Accelerated Mobile Pages
- Building Business Websites with Squarespace 7(Second Edition)
- MySQL數(shù)據(jù)庫應(yīng)用實戰(zhàn)教程(慕課版)
- ASP.NET Core and Angular 2