布尔

Boolean 类型表示可以有 truefalse 两个值的布尔对象。 Boolean has a nullable counterpart declared as Boolean?.

On the JVM, booleans stored as the primitive boolean type typically use 8 bits.

布尔值的内置运算有:

  • ||——析取(逻辑
  • &&——合取(逻辑
  • !——否定(逻辑

For example:

fun main() {
//sampleStart
    val myTrue: Boolean = true
    val myFalse: Boolean = false
    val boolNull: Boolean? = null

    println(myTrue || myFalse)
    // true
    println(myTrue && myFalse)
    // false
    println(!myTrue)
    // false
    println(boolNull)
    // null
//sampleEnd
}

The || and && operators work lazily, which means:

  • If the first operand is true, the || operator does not evaluate the second operand.
  • If the first operand is false, the && operator does not evaluate the second operand.

在 JVM 平台,布尔对象的可空引用是装箱的 Java 类,类似于数字