当前位置:网站首页>Kotlin variables and constants
Kotlin variables and constants
2022-07-26 08:26:00 【Peace of mind】

Kotlin Variable and constant mind map
Catalog
The standard form of declaring variables
The standard form of declaring variables
use var Declare variables
A standard format :var Variable name : type = value
var The declared variable is equivalent to Java Ordinary variables in , It's both readable and writable
When used, the standard format can simplify , For example, you can simplify types , from Kotlin The interpreter infers the type from the value , You can also omit the value ( Member variables in a class generally need to be initialized , If not initialized at the declaration , need lateinit Subsequent initialization ) lateinit var Variable name : type
var s:Int=123 var a:Double var b="123" a=123.4 class A{ lateinit var s:String }
use val Declare variables
A standard format :val Variable name : type = value
val The declared variable is equivalent to Java Medium final Variable , Only read , It cannot be changed after initialization
and var similar , The format can also be simplified , Available when used within a class by lazy Delay initial
class A{ val name:String by lazy { "123" } } fun main(args:Array<String>){ val s=123 var a:Double //a=123 Report errors }
Member variables
Variables declared within a class are member variables
Access right var/val Variable name : type = value
Member variables generally have to be initialized , If you need to assign a value to this variable in subsequent code , be
var Type variable adopts lateinit The way : lateinit var Variable name : type
val use by lazy How to delay initialization :val Variable name : type by lazy{}
lateinit Used only for var Type variable , And can only be used for variables of non basic types , And cannot be used for nullable variables , Type must be specified
by lazy It can only be used for val Type variable , Type must be specified
Nullable variable
stay Kotlin in , For non nullable variables , Its value must not be null, Therefore, there is no need to null non nullable variables
For nullable variables , It has a fixed form of definition , Nullable variables must specify the type , Its value can be a definite value , But null
The format is var/val Variable name type ?= value At this time, the type of variable is type ?
Use :
1. if ... else ...
if ( Variable ==null) { }else{ }
2. ?. And chain use
Variable ?. attribute / Method When variables !=null when , Execute the corresponding properties and methods , Otherwise return to null
3. let The operator
Variable ?.let{} When variables !=null When the {} Inner statement amount to if( Variable !=null){ }
4. Function returns nullable variable
When the return value of the function is of type type ? when , The return type of the function also needs to be : type ?
5. Elvis The operator
(1)?: Usually and ?. In combination with When ?: The previous value of is null when , use ?: The value after... Is used
(2)!! When variables are null An exception will be reported
(3)as? When it cannot be automatically converted to ? Return null
const val author="Aurora"// Top level declaration constant fun getLength(s:String?): Int? {// Return nullable type value , The return type of the function also needs to be nullable type ? return s?.length } fun main(args:Array<String>){ var name:String ?="123454" var address:String ?=null // if ... else ... if (name!=null) println(name)//123454 else println("name is null") if (address!=null) println(address) else println("name is null") // ?. println(name?.length)//6 println(address?.reversed())//null // let The operator name?.let { println(name) }//123454 address?.let{ println(address)}// No printing //Elvis The operator var length=name?.length?:-1 //length=address!!.length// abnormal NullPointerException var a:Int ?=name?.length as? Int println(a)//6 }
Constant
val What is declared is still a variable , It's just not modifiable
stay Java The constant in is const final Therefore, in Kotlin The medium constant is also const val Variable name : type = value
Where constants are used :
1. Top level statement : The top layer is the outermost layer of the entire file , That is to say, in the general category 、 Functions are not top-level
2. Object declaration : stay object Declared in a singleton class decorated
3. Declare in the companion object ( Singleton classes and associated objects will be introduced in the blog later )
边栏推荐
- Burp Suite-第八章 如何使用Burp Intruder
- On some concepts involved in journal papers compilation + journal query methods
- 2022/7/7 exam summary
- Seq2seq and attention model learning notes
- Special lecture 2 dynamic planning learning experience (should be updated for a long time)
- A little awesome, 130000 a month+
- One click deployment lamp and LNMP architecture
- Guitar staff link Jasmine
- Spotty music data client_ ID account
- 正则表达式作业
猜你喜欢

Lesson 3: gcc compiler

22-07-12 personal training match 1 competition experience

shell编程

Nodejs2day(nodejs的模块化,npm下载包,模块加载机制)

R language foundation
Share high voltage ultra low noise LDO test results
![[GUI] swing package (window, pop-up window, label, panel, button, list, text box)](/img/05/8e7483768a4ad2036497cac136b77d.png)
[GUI] swing package (window, pop-up window, label, panel, button, list, text box)

Recurrence of strtus2 historical vulnerability

Add in the registry right click to open in vscode

Take out brother is the biggest support in this society
随机推荐
2022/7/9 exam summary
Data validation typeerror: qiao Validate is not a function
第三天作业
Vscode domestic image server acceleration
[June 29, 2022] examination summary
Burp suite Chapter 7 how to use burp scanner
Differences and connections of previewkeydown, Keydown, keypress and Keyup in C WinForm
memorandum...
Burp suite Chapter 3 how to use burp suite agent
An empirical study on urban unemployment in Guangxi (Macroeconomics)
Official Oracle document
How WPS sets page headers page by page
matplotlib学习笔记
Awk operation
22-07-12 personal training match 1 competition experience
Dev gridcontrol 捕获按键事件
Shell homework the next day
C# WinForm中PreviewKeyDown、KeyDown、KeyPress、KeyUp区别与联系
SPSS用KMeans、两阶段聚类、RFM模型在P2P网络金融研究借款人、出款人行为规律数据
Burp Suite-第七章 如何使用Burp Scanner