|
1 | 1 | [//]: # (title: Booleans) |
| 2 | +[//]: # (description: Learn how to use Boolean values in Kotlin, including declaration, logical operators, and conditions.) |
2 | 3 |
|
3 | | -The type `Boolean` represents boolean objects that can have two values: `true` and `false`. |
4 | | -`Boolean` has a [nullable](null-safety.md) counterpart declared as `Boolean?`. |
| 4 | +<show-structure depth="1"/> |
5 | 5 |
|
6 | | -> On the JVM, booleans stored as the primitive `boolean` type typically use 8 bits. |
| 6 | +The [`Boolean`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/) type represents logical values. |
| 7 | + |
| 8 | +A `Boolean` has one of two values: `true` or `false`. |
| 9 | + |
| 10 | +Use `Boolean` values in comparisons, conditions, loops, and functions that answer yes-or-no questions. |
| 11 | + |
| 12 | + |
| 13 | +## Declare `Boolean` |
| 14 | + |
| 15 | +To declare a `Boolean`, assign it `true` or `false`. |
| 16 | + |
| 17 | +You can specify the `Boolean` type explicitly or let Kotlin infer it from the value: |
| 18 | + |
| 19 | +```kotlin |
| 20 | +val isTrue: Boolean = true |
| 21 | +val isFalse = false // Kotlin infers Boolean |
| 22 | +``` |
| 23 | + |
| 24 | +If a value can be `null`, use `Boolean?`: |
| 25 | + |
| 26 | +```kotlin |
| 27 | +val isEnabled: Boolean? = null |
| 28 | +``` |
| 29 | + |
| 30 | +> You cannot assign an integer value to a `Boolean` variable. |
| 31 | +> In Kotlin, `0` and `1` are not `Boolean` values. |
7 | 32 | > |
8 | 33 | {style="note"} |
9 | 34 |
|
10 | | -Built-in operations on booleans include: |
| 35 | +## Produce `Boolean` values |
| 36 | + |
| 37 | +You can use comparison expressions and functions to produce `Boolean` values: |
| 38 | + |
| 39 | +```kotlin |
| 40 | +val number = 10 |
| 41 | +val isPositive = number > 0 // true |
| 42 | + |
| 43 | +val language = "Kotlin" |
| 44 | +val isEmpty = language.isEmpty() // false |
| 45 | +``` |
11 | 46 |
|
12 | | -* `||` – disjunction (logical _OR_) |
13 | | -* `&&` – conjunction (logical _AND_) |
14 | | -* `!` – negation (logical _NOT_) |
| 47 | +You can use the results in conditions and other expressions. |
| 48 | + |
| 49 | +>Learn more about `Boolean` functions in the [API Reference](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/). |
| 50 | +> |
| 51 | +{style="tip"} |
15 | 52 |
|
16 | | -For example: |
| 53 | +## `Boolean` operators |
| 54 | + |
| 55 | +Use logical operators to combine or invert `Boolean` values. |
| 56 | + |
| 57 | +### Negation (NOT) |
| 58 | + |
| 59 | +Negation inverts a `Boolean` value. |
| 60 | + |
| 61 | +Use the `!` operator: |
| 62 | + |
| 63 | +```kotlin |
| 64 | +val isOn = true |
| 65 | +val isOff = !isOn // isOff is false |
| 66 | +``` |
| 67 | + |
| 68 | +### Logical AND |
| 69 | + |
| 70 | +AND returns `true` only if both operands are `true`. |
| 71 | + |
| 72 | +Use the `&&` operator: |
| 73 | + |
| 74 | +```kotlin |
| 75 | +val a = false && false // false |
| 76 | +val b = false && true // false |
| 77 | +val c = true && false // false |
| 78 | +val d = true && true // true |
| 79 | +``` |
| 80 | + |
| 81 | +> If the first operand is `false`, the `&&` operator does not evaluate the second operand. |
| 82 | +> |
| 83 | +{style="tip"} |
| 84 | + |
| 85 | +### Logical OR |
| 86 | + |
| 87 | +OR returns `true` if at least one operand is `true`. |
| 88 | + |
| 89 | +Use the `||` operator: |
| 90 | + |
| 91 | +```kotlin |
| 92 | +val a = false || false // false |
| 93 | +val b = false || true // true |
| 94 | +val c = true || false // true |
| 95 | +val d = true || true // true |
| 96 | +``` |
| 97 | + |
| 98 | +> If the first operand is `true`, the `||` operator does not evaluate the second operand. |
| 99 | +> |
| 100 | +{style="tip"} |
| 101 | + |
| 102 | +### Exclusive OR (XOR) |
| 103 | + |
| 104 | +XOR returns `true` if the operands have different values. |
| 105 | + |
| 106 | +Use the `xor` infix function: |
| 107 | + |
| 108 | +```kotlin |
| 109 | +val a = false xor false // false |
| 110 | +val b = false xor true // true |
| 111 | +val c = true xor false // true |
| 112 | +val d = true xor true // false |
| 113 | +``` |
| 114 | + |
| 115 | +## Operator precedence |
| 116 | + |
| 117 | +Logical operators follow precedence rules. They determine how to group variables in |
| 118 | +the absence of parentheses: |
| 119 | + |
| 120 | +1. `!` |
| 121 | +2. `xor` |
| 122 | +3. `&&` |
| 123 | +4. `||` |
| 124 | + |
| 125 | +In the following example, the compiler evaluates `&&` before `||`: |
17 | 126 |
|
18 | 127 | ```kotlin |
19 | 128 | fun main() { |
20 | 129 | //sampleStart |
21 | | - val myTrue: Boolean = true |
22 | | - val myFalse: Boolean = false |
23 | | - val boolNull: Boolean? = null |
24 | | - |
25 | | - println(myTrue || myFalse) |
26 | | - // true |
27 | | - println(myTrue && myFalse) |
28 | | - // false |
29 | | - println(!myTrue) |
30 | | - // false |
31 | | - println(boolNull) |
32 | | - // null |
33 | | -//sampleEnd |
| 130 | + val result = true || false && false |
| 131 | + println(result) // true |
| 132 | +//sampleEnd |
34 | 133 | } |
35 | 134 | ``` |
36 | 135 | {kotlin-runnable="true" kotlin-min-compiler-version="1.3"} |
37 | 136 |
|
38 | | -The `||` and `&&` operators work lazily, which means: |
| 137 | +> Use parentheses to make evaluation order explicit. |
| 138 | +> |
| 139 | +{style="tip"} |
| 140 | + |
| 141 | +## `Boolean` in conditions |
39 | 142 |
|
40 | | -* If the first operand is `true`, the `||` operator does not evaluate the second operand. |
41 | | -* If the first operand is `false`, the `&&` operator does not evaluate the second operand. |
| 143 | +You can use `Boolean` expressions to control program flow with `if`, `when`, and loops. |
| 144 | + |
| 145 | +### `if` expressions |
| 146 | + |
| 147 | +```kotlin |
| 148 | +fun main() { |
| 149 | +//sampleStart |
| 150 | + val number = 4 |
| 151 | + val isEven = number % 2 == 0 |
| 152 | + |
| 153 | + if (isEven) { |
| 154 | + println("The number is even.") |
| 155 | + } else { |
| 156 | + println("The number is odd.") |
| 157 | + } |
| 158 | +//sampleEnd |
| 159 | +} |
| 160 | +``` |
| 161 | +{kotlin-runnable="true" kotlin-min-compiler-version="1.3"} |
42 | 162 |
|
43 | | -> On the JVM, nullable references to boolean objects are boxed in Java classes, just like with [numbers](numbers.md#boxing-and-caching-numbers-on-the-java-virtual-machine). |
| 163 | +> Since the condition already has the `Boolean` type, you do not need to compare it to `true` or `false`. |
44 | 164 | > |
45 | | -{style="note"} |
| 165 | +{style="tip"} |
| 166 | + |
| 167 | +### `when` expressions |
| 168 | + |
| 169 | +```kotlin |
| 170 | +fun main() { |
| 171 | +//sampleStart |
| 172 | + val number = 3 |
| 173 | + |
| 174 | + when { |
| 175 | + number > 0 -> println("The number is positive.") |
| 176 | + number < 0 -> println("The number is negative.") |
| 177 | + else -> println("The number is zero.") |
| 178 | + } |
| 179 | +//sampleEnd |
| 180 | +} |
| 181 | +``` |
| 182 | +{kotlin-runnable="true" kotlin-min-compiler-version="1.3"} |
| 183 | + |
| 184 | +### Loops |
| 185 | + |
| 186 | +```kotlin |
| 187 | +fun main() { |
| 188 | +//sampleStart |
| 189 | + var isCalculating = true |
| 190 | + |
| 191 | + while (isCalculating) { |
| 192 | + println("Calculating...") |
| 193 | + isCalculating = false |
| 194 | + } |
| 195 | +//sampleEnd |
| 196 | +} |
| 197 | +``` |
| 198 | +{kotlin-runnable="true" kotlin-min-compiler-version="1.3"} |
0 commit comments