Kotlin Help

Booleans

The type Boolean represents boolean objects that can have two values: true and false. Boolean has a nullable counterpart declared as Boolean?.

Built-in operations on booleans include:

  • || – disjunction (logical OR)

  • && – conjunction (logical AND)

  • ! – negation (logical NOT)

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.

Last modified: 16 February 2024