当前位置:网站首页>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 !

  1. Monocular prefix operator
  2. Self addition and self subtraction operators
  3. Arithmetic operator
  4. in Operator
  5. Generalized assignment operators
  6. Equality and inequality operators
  7. Comparison operator
  8. Closed interval operators
  9. Semi open interval operator 、 Reverse interval 、 Interval step size

Monocular prefix operator

Operator Corresponding method
+aa.unaryPlus()
-aa.unaryMinus()
!aa.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 + ba.plus(b)
a - ba.minus(b)
a * ba.times(b)
a / ba.div(b)
a % ba.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
ina.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 += ba.plusAssign(b)
a -= ba.minusAssign(b)
a *= ba.timesAssign(b)
a /= ba.divAssign(b)
a %= ba.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 == ba?.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 > ba.compareTo(b) > 0
a < ba.compareTo(b) < 0
a >= ba.compareTo(b) >= 0
a <= ba.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
原网站

版权声明
本文为[Captain.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270558593575.html