当前位置:网站首页>Kotlin learning notes (1)
Kotlin learning notes (1)
2022-06-13 06:00:00 【Captain.】
Kotlin Operator
Preface : Google I/O 2019 The conference announce Kotlin first !
Android Development will be more and more based on Kotlin Mainly .
What are you waiting for , Hurry to learn .
Kotlin Operator And Java Operator differences .
Learning record !
- Monocular prefix operator
- Self addition and self subtraction operators
- Arithmetic operator
- in Operator
- Generalized assignment operators
- Equality and inequality operators
- Comparison operator
- Closed interval operators
- Semi open interval operator 、 Reverse interval 、 Interval step size
Monocular prefix operator
Operator | Corresponding method |
---|---|
+a | a.unaryPlus() |
-a | a.unaryMinus() |
!a | a.not() |
Be careful :
1.kotlin Functions of are implemented in the form of methods
2.kotlin There is no three digit operator
java edition :
int a=10;
System.out.print(+a);
kotlin edition :
var a=10
println(a.unaryMinus())
Output results : 10
-a,!a It's written the same way I'm not going to post the code here !
Self addition and self subtraction operators
Operator | Corresponding method |
---|---|
a++ | a.inc() |
a- - | a.dec() |
java edition :
int a=10;
System.out.print(a++);
kotlin edition :
var a=10
println(a.inc())
Output results : 11
Empathy :println(a.inc()) Replace with println(a.dec()) Output results : 9
Arithmetic operator
Be careful : Pay attention to operator priority when using
Operator | Corresponding method |
---|---|
a + b | a.plus(b) |
a - b | a.minus(b) |
a * b | a.times(b) |
a / b | a.div(b) |
a % b | a.rem(b) /* mod( Abandoning )*/ |
java edition :
int a=10;
int b=2;
System.out.print(a+b);
kotlin edition :
var a=10
var b=2
println(a.plus(b))
Output results : 12
Empathy :println(a.plus()) Replace with println(a.minus()) etc. And so on
in Operator
Operator | Corresponding method |
---|---|
in | a.contains() |
!in | !a.contains() |
java edition :
String a="kotlinAndroid";
System.out.println(a.contains("java"));
System.out.println(!a.contains("java"));
kotlin edition :
var a="kotlinAndroid"
var flag1="java" in a
var flag2="java" !in a
println(flag1)
println(flag2)
flag1 Output :false flag2 Output :true
Be careful : in It can also be used to traverse Let's talk later .
Generalized assignment operators
Operator | Corresponding method |
---|---|
a += b | a.plusAssign(b) |
a -= b | a.minusAssign(b) |
a *= b | a.timesAssign(b) |
a /= b | a.divAssign(b) |
a %= b | a.remAssign(b) |
Be careful :
plusAssign Method is not available by default // Tips :kotlin Operators can overload adopt operator Modifier
plusAssign() Return value... Is not allowed , Otherwise, an error is reported .
If plusAssign() Method does not exist ,a+=b The default is a=a+b.
Equality and inequality operators
Operator | Corresponding method |
---|---|
a == b | a?.equals(b) ?: (b === null) |
a != b | !(a?.equals(b) ?: (b === null) |
These operators only use functions equals(other: Any?): Boolean, It can be overridden to provide a custom equality detection implementation . No other function with the same name will be called ( Such as equals(other: Foo)).
Be careful :=== and !==( Identity check ) Do not overload , Therefore, there is no agreement with them .
This == There are some special operators : It is translated into a complex expression , For screening null value . null == null Always true, For non empty x,x == null Always false Instead of calling x.equals().
Comparison operator
Operator | Corresponding method |
---|---|
a > b | a.compareTo(b) > 0 |
a < b | a.compareTo(b) < 0 |
a >= b | a.compareTo(b) >= 0 |
a <= b | a.compareTo(b) < =0 |
All comparisons are converted to true compareTo Call to , This function needs to return Int value
Closed interval operators
a~b The closed interval operator of is a…b( Include a and b)
kotlin Code :
for (index in 1..10){
println(index)
}
Output :
1
2
3
4
5
6
7
8
9
10
Semi open interval operator
It is convenient to traverse the array , By keyword until
a until b ( Include a But not including b Only to b-1)
kotlin Code :
fun main(args: Array<String>) {
var data = arrayOf("java", "android", "kotlin")
for (index in 0 until data.size) {
println(data[index])
}
}
Output :
java
android
kotlin
Reverse interval
You can grow up through a…b Interval operator
Then you can use it from big to small a…b Do you ?
The result is impossible !!( No problem compiling ! But there's no output !!)
kotlin Provided keyword downTo Used to achieve from large to small
fun main(args: Array<String>) {
for (index in 6 downTo 1) {
println(index)
}
}
Output :
6
5
4
3
2
1
Interval step size
Interval if you don't step The default interval step size is 1
Take a chestnut :123456789
Each increment 1,1 That's it. step
So suppose I now 1~10, Each increment 2 Well ?
fun main(args: Array<String>) {
for (index in 1..10 step 2) {
println(index)
}
}
Output :
1
3
5
7
9
边栏推荐
- Mongodb multi field aggregation group by
- MySQL stored procedure
- [spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib
- Basic application of sentinel series
- Quartz database storage
- You still can't remotely debug idea? Come and have a look at my article. It's easy to use
- MongoDB 多字段聚合Group by
- Exception after repeated application redeployment on tongweb: application instance has been stopped already or outofmemoryerror:metaspace
- The 13th week of the second semester of sophomore year
- Leetcode- hex number - simple
猜你喜欢
Misunderstanding of tongweb due to ease of use
Application virtual directory static resource configuration on tongweb
OpenGL Mosaic (8)
How slow is the application system on tongweb? How dead is it?
Ffmpeg download suffix is Video files for m3u8
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
17 servicetask of flowable task
20 flowable container (event sub process, things, sub process, pool and pool)
Config server configuration center of Nacos series
OpenGL马赛克(八)
随机推荐
2021.9.29 learning log restful architecture
2021.9.29学习日志-Restful架构
MySQL fuzzy query and sorting by matching degree
Leetcode judge subsequence simple
AUTOSAR actual combat tutorial pdf version
The SQL file of mysql8.0 was imported into version 5.5. There was a pit
3. Postman easy to use
Current limiting and fusing of gateway gateway in Spirng cloud
How to set the import / export template to global text format according to the framework = (solve the problem of scientific counting)
MySQL stored procedure
Service fusing and degradation of Note Series
August 15, 2021 another week
11 signalthrowingevent and signalboundaryevent of flowable signal event
Class conflicts caused by tongweb Enterprise Edition and embedded Edition
OpenGL马赛克(八)
Leetcode- find a difference - simple
Getclassloader() returns null, getclassloader() gets null
Service architecture diagram of Nacos series
Leetcode- reverse string - simple
1016 part a+b (15 points)