📜  Kotlin 运算符

📅  最后修改于: 2022-05-13 01:54:58.384000             🧑  作者: Mango

Kotlin 运算符

运算符是对操作数执行不同操作的特殊符号。例如 + 和 - 是分别执行加法和减法的运算符。与Java一样,Kotlin 包含不同种类的运算符。

  • 算术运算运算符
  • 关系运算符
  • 赋值运算符
  • 一元运算符
  • 逻辑运算符
  • 位运算符

算术运算符 –

OperatorsMeaningExpressionTranslate to
+Additiona + ba.plus(b)
Subtractiona – ba.minus(b)
*Multiplicationa * ba.times(b)
/Divisiona / ba.div(b)
%Modulusa % ba.rem(b)

Kotlin
fun main(args: Array)
{
    var a = 20 var b = 4 println("a + b = " + (a + b))
        println("a - b = " + (a - b))
        println("a * b = " + (a.times(b)))
        println("a / b = " + (a / b))
        println("a % b = " + (a.rem(b)))
}


Kotlin
fun main(args: Array)
{
    var c = 30
    var d = 40
    println("c > d = "+(c>d))
    println("c < d = "+(c.compareTo(d) < 0))
    println("c >= d = "+(c>=d))
    println("c <= d = "+(c.compareTo(d) <= 0))
    println("c == d = "+(c==d))
    println("c != d = "+(!(c?.equals(d) ?: (d === null))))
}


Kotlin
fun main(args : Array){
 var a = 10
    var b = 5
    a+=b
    println(a)
    a-=b
    println(a)
    a*=b
    println(a)
    a/=b
    println(a)
    a%=b
    println(a)
 
}


Kotlin
fun main(args : Array){
    var e=10
    var flag = true
    println("First print then increment: "+ e++)
    println("First increment then print: "+ ++e)
    println("First print then decrement: "+ e--)
    println("First decrement then print: "+ --e)
}


Kotlin
fun main(args : Array){
    var x = 100
    var y = 25
    var z = 10
    var result = false
    if(x > y && x > z)
     println(x)
    if(x < y || x > z)
     println(y)
    if( result.not())
     println("Logical operators")
}


Kotlin
fun main(args: Array)
{
    println("5 signed shift left by 1 bit: " + 5.shl(1))
    println("10 signed shift right by 2 bits: : " + 10.shr(2))
    println("12 unsigned shift right by 2 bits:  " + 12.ushr(2))
    println("36 bitwise and 22: " + 36.and(22))
    println("36 bitwise or 22: " + 36.or(22))
    println("36 bitwise xor 22: " + 36.xor(22))
    println("14 bitwise inverse is: " + 14.inv())
}


输出:

a + b = 24
a - b = 16
a * b = 80
a / b = 5
a % b = 0


关系运算符 –

OperatorsMeaningExpressionTranslate to
>greater thana > ba.compareTo(b) > 0
<less thana < ba.compareTo(b) < 0
>=greater than or equal toa >= ba.compareTo(b) >= 0
<=less than or equal toa <= ba.compareTo(b) <= 0
==is equal toa == ba?.equals(b) ?: (b === null)
!=not equal toa != b!(a?.equals(b) ?: (b === null)) > 0

科特林

fun main(args: Array)
{
    var c = 30
    var d = 40
    println("c > d = "+(c>d))
    println("c < d = "+(c.compareTo(d) < 0))
    println("c >= d = "+(c>=d))
    println("c <= d = "+(c.compareTo(d) <= 0))
    println("c == d = "+(c==d))
    println("c != d = "+(!(c?.equals(d) ?: (d === null))))
}

输出:

c > d = false
c < d = true
c >= d = false
c <= d = true
c == d = false
c != d = true

赋值运算符 –

OperatorsExpressionTranslate to
+=a = a + ba.plusAssign(b) > 0
-=a = a – ba.minusAssign(b) < 0
*=a = a * ba.timesAssign(b)>= 0
/=a = a / ba.divAssign(b) <= 0
%=a = a % ba.remAssign(b)

科特林

fun main(args : Array){
 var a = 10
    var b = 5
    a+=b
    println(a)
    a-=b
    println(a)
    a*=b
    println(a)
    a/=b
    println(a)
    a%=b
    println(a)
 
}

输出:

15
10
50
10
0

一元运算符 –

OperatorsExpressionTranslate to
++++a or a++a.inc()
–a or a–a.dec()

科特林

fun main(args : Array){
    var e=10
    var flag = true
    println("First print then increment: "+ e++)
    println("First increment then print: "+ ++e)
    println("First print then decrement: "+ e--)
    println("First decrement then print: "+ --e)
}

输出:

First print then increment: 10
First increment then print: 12
First print then decrement: 12
First decrement then print: 10

逻辑运算符 –

OperatorsMeaningExpression
&&return true if all expressions are true(a>b) && (a>c)
||return true if any of expression is true(a>b) || (a>c)
!return complement of the expressiona.not()

科特林

fun main(args : Array){
    var x = 100
    var y = 25
    var z = 10
    var result = false
    if(x > y && x > z)
     println(x)
    if(x < y || x > z)
     println(y)
    if( result.not())
     println("Logical operators")
}

输出:

100
25
Logical operators

位运算符 –

OperatorsMeaningExpression
shlsigned shift lefta.shl(b)
shrsigned shift righta.shr(b)
ushrunsigned shift righta.ushr()
andbitwise anda.and(b)
orbitwise ora.or()
xorbitwise xora.xor()
invbitwise inversea.inv()

科特林

fun main(args: Array)
{
    println("5 signed shift left by 1 bit: " + 5.shl(1))
    println("10 signed shift right by 2 bits: : " + 10.shr(2))
    println("12 unsigned shift right by 2 bits:  " + 12.ushr(2))
    println("36 bitwise and 22: " + 36.and(22))
    println("36 bitwise or 22: " + 36.or(22))
    println("36 bitwise xor 22: " + 36.xor(22))
    println("14 bitwise inverse is: " + 14.inv())
}

输出:

5 signed shift left by 1 bit: 10
10 signed shift right by 2 bits: : 2
12 unsigned shift right by 2 bits:  3
36 bitwise and 22: 4
36 bitwise or 22: 54
36 bitwise xor 22: 50
14 bitwise inverse is: -15