Kotlin 简明教程

Kotlin - Booleans

很多时候,我们遇到这样的情况,我们都可以在 YesNo 之间做出决定,或者可能可以说 TrueFalse 。为了处理这种情况,Kotlin 有一个布尔数据类型,它可以取值 truefalse

Many times we come across a situation where we need to take decision in Yes or No, or may be we can say True or False. To handle such situation Kotlin has a Boolean data type, which can take the values either true or false.

Kotlin 也有一个可以具有 nullable 值的 Boolean? 对应项。

Kotlin also has a nullable counterpart Boolean? that can have the null value.

Create Boolean Variables

可以使用 Boolean 关键字创建布尔变量,并且此变量只能取值 truefalse

A boolean variable can be created using Boolean keyword and this variable can only take the values either true or false:

Example

fun main(args: Array<String>) {
   val isSummer: Boolean = true
   val isCold: Boolean = false

   println(isSummer)
   println(isCold)

}

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

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

true
false

事实上,我们可以不使用 Boolean 关键字创建 Kotlin 布尔变量,Kotlin 会根据分配的值 truefalse 理解变量类型。

In fact, we can create Kotlin boolean variables without using Boolean keyword and Kotlin will understand the variable type based on the assigned values either true or false

Kotlin Boolean Operators

Kotlin 为布尔变量提供了以下 built-in 运算符。这些运算符也称为逻辑运算符:

Kotlin provides following built-in operators for boolean variables. These operators are also called 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

Kotlin Boolean Expression

布尔表达式会返回 truefalse 值,主要用于使用 if…​else 表达式检查条件。布尔表达式使用关系运算符,例如 >, <, >= 等。

A Boolean expression returns either true or false value and majorly used in checking the condition with if…​else expressions. A boolean expression makes use of relational operators, for example >, <, >= etc.

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

Kotlin and()and or() Functions

Kotlin 提供了 and()or() 函数来执行两个布尔操作数之间的逻辑 AND 和逻辑 OR 运算。

Kotlin provides and() and or() functions to perform logical AND and logical OR operations between two boolean operands.

这些函数不同于 &&|| 运算符,因为这些函数不执行短路求值,而是始终计算两个操作数。

These functions are different from && and || operators because these functions do not perform short-circuit evaluation but they always evaluate both the operands.

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

   println("x.and(y) = " +  x.and(y))
   println("x.or(y) = " +  x.or(y))
   println("x.and(z) = " +  x.and(z))
}

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

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

x.and(y) = false
x.or(y) = true
x.and(z) = true

Kotlin 还提供 not()xor() 函数来执行逻辑 NOTXOR 运算。

Kotlin also provides not() and xor() functions to perform Logical NOT and XOR operations respectively.

Boolean to String

您可以使用 toString() 函数将布尔对象转换为其等效的字符串表示形式。

You can use toString() function to convert a Boolean object into its equivalent string representation.

在字符串变量中分配 truefalse 值时,您将需要此转换。

You will need this conversion when assigning a true or false value in a String variable.

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

   z = x.toString()

   println("x.toString() = " +  x.toString())
   println("z = " +  z)
}

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

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

x.toString() = true
z = true