directory title
- Some compound symbols commonly used in Kotlin ( '?' '?:' '!!' '::' '. .' 'as?' )
-
- 1. [ : ] ------ colon operator
- 2. [ ? ] ------ question mark
- 3. [ !! ] ------ double exclamation mark operator
- 4. [ ?: ] ------ exclamation point colon operator (Elvis)
- 5. [ :: ] ------ double colon operator
- 6. [ as ] ------ type conversion operator
- 7. [ . . ] ------ double dot interval operator
- 8. [ -> ] ------ middle bar greater than sign operator
- 9. [ == and === ] ------ equal sign operator
- 10. [ is ] ------ belongs to the operator
- 11. [ $ ] ------ template operator
- What is Kotlin?
Kotlin [Kotlin] is a static programming language for modern multi-platform applications, developed by JetBrains;
Kotlin is a static language that supports multiple platforms, including mobile, server, and browser;
Kotlin is also a A language that combines object-oriented and functional programming, supports generics, safe null judgments, and Kotlin and Java can achieve complete interaction.In Google I/O 2017, Google announced Kotlin as the official Android development language.
The role of Kotlin:
① Conciseness: Greatly reduce the amount of boilerplate code.
②Security: Avoid the entire class of errors such as null pointer exceptions.
③Interoperability: Make full use of the existing libraries of JVM, Android and browsers.
④Tool-friendly: It can be built with any Java IDE or using the command line.
Some compound symbols commonly used in Kotlin ( '?' '?:' '!!' '::' '. .' 'as?' )
1. [ : ] ------ colon operator
" : "
Operators are used to define variables, class inheritance, etc.
" : " colon followed by reference type.
- " : " Operators are used to define variables, class inheritance, etc.
//定义变量
var name: String
//继承类
class MainActivity : AppCompatActivity()
2. [ ? ] ------ question mark
" ?"
Modified after the type of the member variable, indicating this variablecan be empty(null), the system will not report its null pointer exception under any circumstances;
" ? " modified behind the object means that if the object is empty (null), the subsequent logic will not be processed;If the variable we define is a nullable type, we need to use 【String?】.
- " ?" Indicates that this object can be empty
//在变量类型后面加上 ? ,代表该变量是可为空
var age: String? = "23"
// 如果 str 不能转为 Int 类型,则返回 null
fun parseInt(str: String): Int? {
// nothing
}
3. [ !! ] ------ double exclamation mark operator
" !! "
Put it after the object or method passing the actual parameter, which meansCan not be empty(null), if it is null, an exception is thrown;
Kotlin does not recommend the use of non-null assertions, usually we use "?: " to prevent the program from crashing due to a null pointer exception when it is running;The usage of " !! " is equivalent to if else in Java to judge whether it is null.
- " !! " Null check mechanism specifier
//非空 b,如果 b 为空,则抛出空指针
val l = b!!.length
4. [ ?: ] ------ exclamation point colon operator (Elvis)
" ?: "
Nullable variables can be specifiedempty time, call the return value of the member method or property in the variable;
its syntax format is: "expression A ?: expression B" .
Returns the value of left-hand expression A if left-hand expression A is non-null, otherwise returns the value of right-hand expression B. The expression B on the right is evaluated if and only if the left is empty.The use of " ?: " is similar to using the ternary operator in Java.
- " ?:" If it is not empty, use it, otherwise return the specified value
//a 不为空,返回 a 的长度,否则返回 -1
val 1ength==a?.length?:-1
//相当于
val length:Int=if(a!=null)a.length else -1
5. [ :: ] ------ double colon operator
" :: "
Indicates that a method is used as a parameter and passed to another method for use. Generally speaking, it refers to a method; it can be used to obtain the Class object of the class;
" :: " The function of the double colon and the method name is to pass the method as a parameter.
- " :: " Can be used to get the Class object of the class
//得到 SecondActivity 对象
startActivity(Intent(this@MainActivity, SecondActivity::class.java))
6. [ as ] ------ type conversion operator
" as "
Indicates that if the type conversion is unsuccessful, the type conversion operator usually throws an exception, so the unsafe type conversion uses the operator as;
when the type conversion fails, it returns null, in order to avoid throwing an exception, you can use the safe type conversion operator as?;" as " converts the value to the given type, or returns null if the type is inappropriate.
7. [ . . ] ------ double dot interval operator
" . . "
Expressions representing ranges (or ranges) are formed by the rangeTo function of the operator form ... supplemented by in and !in.
The in operator means that i is in a certain interval class; !in means that i is not in
a certain interval class ; . The interval from a to b includes a and does not include b; downTo() function: loop in the flashback interval, and iterates the numbers in the interval from a to b; step() function: jumping function, parity judgment selection, you can specify any step size.
// 输出数字 “ 1 到 10 ”
if (i in 1..10) {
print(i)}
// 输出数字 “ 1 到 9 ”同[1,10)
for (i in 1 until 10) {
print(i)}
// 输出数字 “54321”
for (i in 5 downTo 1) print(i)
// 输出数字 “ 135 ”
for (i in 1..5 step 2) {
print(i)}
// 输出数字 “ 531 ”
for (i in 5 downTo 1 step 2) {
print(i)}
// 如果 i 在 1 到 10 十个数之间,就输出 i
if(i in 1..10){
print (i)
}
// 如果 i 不在 1 到 10 十个数之间,就输出 i
if(i !in 1..10){
print (i)
}
// 什么都不输出,注意这种递减情况建议使用 until 或 downTo
for (i in 4..1){
print(i)
}
// 如果 i 在 1 到 10 九个数之间(不包含 10),就输出 i
if(i in 1 until 10){
print (i)
}
8. [ -> ] ------ middle bar greater than sign operator
" -> "
From a morphological point of view, it is aflow and correspondenceThe relationship;
means that after the previous statement is executed, the execution flow will be transferred to the pointed statement, and it is corresponding.
9. [ == and === ] ------ equal sign operator
== Judgmentvalueis equal, compares twovaluesize.
=== Judgmentvalue and referenceare exactly equal, compare twoobjectaddress.
var a:Int = 10
print(a===a) //结果:true
var b:Int =a
var c:Int =a
print(b===c) //结果:false
print(b==c) //结果:true
10. [ is ] ------ belongs to the operator
" is "
This operator can be used to determine whether an instancebelonganother instance
//name 是否是 String 类型
if (name is String){
print(true)
}
11. [ $ ] ------ template operator
" $ "
This template operator canoutputA variable value, which is equivalent to concatenating strings.
// 输出结果为 My name is GuYu
var name="GuYu"
print("My name is $name")
Kotlin files are suffixed with .kt; its basic mind map is as follows:
-
Recommended reference link:
https://www.cnblogs.com/shen-hua/category/1141903.html
https://jetictors.github.io/categories/Kotlin/page/2/
Note:
Likes, comments, and reprints are welcome. Please give the link to the original text in an obvious place on the article page
. Those who know, thank you for reading my article in the vast crowd.
Where is the signature without personality!
For details, please follow me
and continue to update
© 2021 09 - Guyu.com | 【Copyright All Rights Reserved】 |