Kotlin 简明教程

Kotlin - Operators

操作符是一个指示编译器执行特定数学或逻辑操作的符号。Kotlin 中内置了丰富的操作符,并提供了以下类型的操作符:

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Kotlin is rich in built-in operators and provide the following types of operators:

  1. Arithmetic Operators

  2. Relational Operators

  3. Assignment Operators

  4. Unary Operators

  5. Logical Operators

  6. Bitwise Operations

现在让我们逐个研究这些 Kotlin 操作符。

Now let’s look into these Kotlin Operators one by one.

(a) Kotlin Arithmetic Operators

Kotlin 算术运算符用于执行基本的数学运算,如加法、减法、乘法和除法等。

Kotlin arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication and division etc.

Operator

Name

Description

Example

+

Addition

Adds together two values

x + y

-

Subtraction

Subtracts one value from another

x - y

*

Multiplication

Multiplies two values

x * y

/

Division

Divides one value by another

x / y

%

Modulus

Returns the division remainder

x % y

Example

以下示例展示了使用 Kotlin 算术运算符的不同运算:

Following example shows different calculations using Kotlin Arithmetic Operators:

fun main(args: Array<String>) {
   val x: Int = 40
   val y: Int = 20

   println("x + y = " +  (x + y))
   println("x - y = " +  (x - y))
   println("x / y = " +  (x / y))
   println("x * y = " +  (x * y))
   println("x % y = " +  (x % y))
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

x + y = 60
x - y = 20
x / y = 2
x * y = 800
x % y = 0

(b) Kotlin Relational Operators

Kotlin 关系(比较)运算符用于比较两个值并返回 Boolean 值: truefalse

Kotlin relational (comparison) operators are used to compare two values, and returns a Boolean value: either true or false.

Operator

Name

Example

>

greater than

x > y

<

less than

x < y

>=

greater than or equal to

x >= y

less than or equal to

x ⇐ y

==

is equal to

x == y

!=

not equal to

x != y

Example

以下示例展示了使用 Kotlin 关系运算符的不同运算:

Following example shows different calculations using Kotlin Relational Operators:

fun main(args: Array<String>) {
   val x: Int = 40
   val y: Int = 20

   println("x > y = " +  (x > y))
   println("x < y = " +  (x < y))
   println("x >= y = " +  (x >= y))
   println("x <= y = " +  (x <= y))
   println("x == y = " +  (x == y))
   println("x != y = " +  (x != y))
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

x > y = true
x < y = false
x >= y = true
x <= y = false
x == y = false
x != y = true

(c) Kotlin Assignment Operators

Kotlin 赋值运算符用于将值赋给变量。

Kotlin assignment operators are used to assign values to variables.

以下是一个示例,其中我们使用赋值运算符 = 将值赋给两个变量:

Following is an example where we used assignment operator = to assign a values into two variables:

fun main(args: Array<String>) {
   val x: Int = 40
   val y: Int = 20

   println("x = " +  x)
   println("y = " +  y)
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

x = 40
y = 20

以下是一个示例,其中我们使用赋值运算符 += 将自身变量的值加起来并将其重新赋给同一变量:

Following is one more example where we used assignment operator += to add the value of self variable and assign it back into the same variable:

fun main(args: Array<String>) {
   var x: Int = 40

   x += 10

   println("x = " +  x)
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

x = 50

以下是所有赋值运算符的列表:

Following is a list of all assignment operators:

Operator

Example

Expanded Form

=

x = 10

x = 10

+=

x += 10

x = x - 10

-=

x -= 10

x = x - 10

*=

x *= 10

x = x * 10

/=

x /= 10

x = x / 10

%=

x %= 10

x = x % 10

Example

以下示例展示了使用 Kotlin 赋值运算符的不同运算:

Following example shows different calculations using Kotlin Assignment Operators:

fun main(args: Array<String>) {
   var x: Int = 40

   x += 5
   println("x += 5 = " + x )

   x = 40;
   x -= 5
   println("x -= 5 = " +  x)

   x = 40
   x *= 5
   println("x *= 5 = " +  x)

   x = 40
   x /= 5
   println("x /= 5 = " +  x)

   x = 43
   x %= 5
   println("x %= 5 = " + x)
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

x += 5 = 45
x -= 5 = 35
x *= 5 = 200
x /= 5 = 8
x %= 5 = 3

(d) Kotlin Unary Operators

一元运算符只需要一个操作数;它们执行各种操作,例如将值递增/递减 1、对表达式求反或反转布尔值的取值。

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

以下是 Kotlin 一元运算符的列表:

Following is the list of Kotlin Unary Operators:

Operator

Name

Example

+

unary plus

+x

-

unary minus

-x

++

increment by 1

++x

 — 

decrement by 1

--x

!

inverts the value of a boolean

!x

Example

以下示例显示了使用 Kotlin 一元运算符进行的不同计算:

Following example shows different calculations using Kotlin Unary Operators:

fun main(args: Array<String>) {
   var x: Int = 40
   var b:Boolean = true

   println("+x = " +  (+x))
   println("-x = " +  (-x))
   println("++x = " +  (++x))
   println("--x = " +  (--x))
   println("!b = " +  (!b))
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

+x = 40
-x = -40
++x = 41
--x = 40
!b = false

这里的增量 () 和递减 (--) 运算符可用作前缀如 x 或 --x 以及后缀如 x++ 或 x--。这两种形式之间的唯一区别是如果我们将其用作前缀,则在执行表达式之前应用运算符,但如果将其用作后缀,则在执行表达式之后应用运算符。

Here increment () and decrement (--) operators can be used as prefix as x or --x as well as suffix as x++ or x--. The only difference between the two forms is that in case we use them as prefix then operator will apply before expression is executed, but if use them as suffix then operator will apply after the expression is executed.

(e) Kotlin Logical Operators

Kotlin 逻辑运算符用于确定两个变量或值之间的逻辑:

Kotlin logical operators are used to determine the logic between two variables or values:

以下是 Kotlin 逻辑运算符的列表:

Following is the list of Kotlin Logical Operators:

Operator

Name

Description

Example

&&

Logical and

Returns true if both operands are true

x && y

Logical or

Returns true if either of the operands is true

x

y

!

Logical not

Reverse the result, returns false if the operand is true

!x

Example

以下示例显示了使用 Kotlin 逻辑运算符的不同计算方法:

Following example shows different calculations using Kotlin Logical Operators:

fun main(args: Array<String>) {
   var x: Boolean = true
   var y:Boolean = false

   println("x && y = " +  (x && y))
   println("x || y = " +  (x || y))
   println("!y = " +  (!y))
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

x && y = false
x || y = true
!y = true

(e) Kotlin Bitwise Operations

Kotlin 没有位运算符,但 Kotlin 提供了一系列辅助函数来执行按位操作。

Kotlin does not have any bitwise operators but Kotlin provides a list of helper functions to perform bitwise operations.

以下是 Kotlin 按位函数的列表:

Following is the list of Kotlin Bitwise Functions:

Function

Description

Example

shl (bits)

signed shift left

x.shl(y)

shr (bits)

signed shift right

x.shr(y)

ushr (bits)

unsigned shift right

x.ushr(y)

and (bits)

bitwise and

x.and(y)

or (bits)

bitwise or

x.or(y)

xor (bits)

bitwise xor

x.xor(y)

inv()

bitwise inverse

x.inv()

Example

以下示例显示了使用 Kotlin 按位函数进行的不同计算:

Following example shows different calculations using Kotlin bitwise functions:

fun main(args: Array<String>) {
   var x:Int = 60	  // 60 = 0011 1100
   var y:Int = 13	  // 13 = 0000 1101
   var z:Int

   z = x.shl(2)       // 240 = 1111 0000
   println("x.shl(2) = " +  z)

   z = x.shr(2)       // 15 = 0000 1111
   println("x.shr(2) = " +  z)

   z = x.and(y)       // 12 = 0000 1100
   println("x.and(y)  = " +  z)

   z = x.or(y)        // 61 = 0011 1101
   println("x.or(y)  = " +  z)

   z = x.xor(y)       // 49 = 0011 0001
   println("x.xor(y)  = " +  z)

   z = x.inv()        // -61 = 1100 0011
   println("x.inv()  = " +  z)
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

x.shl(2) = 240
x.shr(2) = 15
x.and(y)  = 12
x.or(y)  = 61
x.xor(y)  = 49
x.inv()  = -61