📅  最后修改于: 2021-01-05 07:13:23             🧑  作者: Mango
运算符是对操作数(值或变量)执行运算的特殊字符。Kotlin中提供了多种运算符。
算术运算运算符用于执行基本的数学运算,例如加法(+),减法(-),乘法(*),除法(/)等。
Operator | Description | Expression | Translate to |
---|---|---|---|
+ | Addition | a+b | a.plus(b) |
– | Subtraction | a-b | a.minus(b) |
* | Multiply | a*b | a.times(b) |
/ | Division | a/b | a.div(b) |
% | Modulus | a%b | a.rem(b) |
fun main(args : Array) {
var a=10;
var b=5;
println(a+b);
println(a-b);
println(a*b);
println(a/b);
println(a%b);
}
输出:
15
5
50
2
0
关系运算符显示关系并在操作数之间进行比较。以下是不同的关系运算符:
Operator | Description | Expression | Translate to |
---|---|---|---|
> | greater than | a>b | a.compateTo(b)>0 |
< | Less than | a | a.compateTo(b)<0 |
>= | greater than or equal to | a>=b | a.compateTo(b)>=0 |
<= | less than or equal to | a<=b | a?.equals(b)?:(b===null) |
== | is equal to | a==b | a?.equals(b)?:(b===null) |
!= | not equal to | a!=b | !(a?.equals(b)?:(b===null)) |
关系运算符示例
fun main(args : Array) {
val a = 5
val b = 10
val max = if (a > b) {
println("a is greater than b.")
a
} else{
println("b is greater than a.")
b
}
println("max = $max")
}
输出:
b is greater than a.
max = 10
赋值运算符“ =”用于将值分配给另一个变量。值的分配从右到左进行。
Operator | Description | Expression | Convert to |
---|---|---|---|
+= | add and assign | a+=b | a.plusAssign(b) |
-= | subtract and assign | a-=b | a.minusAssign(b) |
*= | multiply and assign | a*=b | a.timesAssign(b) |
/= | divide and assign | a/=b | a.divAssign(b) |
%= | mod and assign | a%=b | a.remAssign(b) |
赋值运算符的示例
fun main(args : Array) {
var a =20;var b=5
a+=b
println("a+=b :"+ a)
a-=b
println("a-=b :"+ a)
a*=b
println("a*=b :"+ a)
a/=b
println("a/=b :"+ a)
a%=b
println("a%=b :"+ a)
}
输出:
a+=b :25
a-=b :20
a*=b :100
a/=b :20
a%=b :0
一元运算符仅与单个操作数一起使用。以下是下面给出的一些一元运算运算符。
Operator | Description | Expression | Convert to |
---|---|---|---|
+ | unary plus | +a | a.unaryPlus() |
– | unary minus | -a | a.unaryMinus() |
++ | increment by 1 | ++a | a.inc() |
— | decrement by 1 | –a | a.dec() |
! | not | !a | a.not() |
一元运算符的示例
fun main(args: Array){
var a=10
var b=5
var flag = true
println("+a :"+ +a)
println("-b :"+ -b)
println("++a :"+ ++a)
println("--b :"+ --b)
println("!flag :"+ !flag)
}
输出:
+a :10
-b :-5
++a :11
--b :4
!flag :false
逻辑运算符用于检查操作数之间的条件。逻辑运算符的列表如下。
Operator | Description | Expression | Convert to |
---|---|---|---|
&& | return true if all expression are true | (a>b) && (a>c) | (a>b) and (a>c) |
|| | return true if any expression are true | (a>b) || (a>c) | (a>b) or(a>c) |
! | return complement of expression | !a | a.not() |
逻辑运算符示例
fun main(args: Array){
var a=10
var b=5
var c=15
var flag = false
var result: Boolean
result = (a>b) && (a>c)
println("(a>b) && (a>c) :"+ result)
result = (a>b) || (a>c)
println("(a>b) || (a>c) :"+ result)
result = !flag
println("!flag :"+ result)
}
输出:
(a>b) && (a>c) :false
(a>b) || (a>c) :true
!flag :true
在Kotlin中,没有任何特殊的按位运算运算符。使用命名函数完成按位运算。
Named Function | Description | Expression |
---|---|---|
shl (bits) | signed shift left | a.shl(b) |
shr (bits) | signed shift right | a.shr(b) |
ushr (bits) | unsigned shift right | a.ushr(b) |
and (bits) | bitwise and | a.and(b) |
or (bits) | bitwise or | a.or(b) |
xor (bits) | bitwise xor | a.xor(b) |
inv() | bitwise inverse | a.inv() |
按位运算示例
fun main(args: Array){
var a=10
var b=2
println("a.shl(b): "+a.shl(b))
println("a.shr(b): "+a.shr(b))
println("a.ushr(b:) "+a.ushr(b))
println("a.and(b): "+a.and(b))
println("a.or(b): "+a.or(b))
println("a.xor(b): "+a.xor(b))
println("a.inv(): "+a.inv())
}
输出:
a.shl(b): 40
a.shr(b): 2
a.ushr(b:) 2
a.and(b): 2
a.or(b): 10
a.xor(b): 8
a.inv(): -11