Skip to content

Latest commit

 

History

History
45 lines (35 loc) · 1.16 KB

booleans.md

File metadata and controls

45 lines (35 loc) · 1.16 KB

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

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

{type="note"}

布尔值的内置运算有:

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

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
}

{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}

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 类,类似于数字

{type="note"}