5 minutes read

In this topic, we'll talk about Booleans. Boolean values are often used in programming. Basically, they represent two opposite states. For example, a boolean value can indicate:

  • whether a store is open;

  • whether a plane is up in the sky;

  • whether travelling is allowed;

  • whether an option is enabled, and so on.

Boolean variables

Boolean is a special data type that has only two possible values — true and false:

val t = true  // t is true
val f = false // f is false

println(t) // true
println(f) // false

You cannot assign an integer value to a Boolean variable. In Kotlin, 0 is not the same as false.

True or False

Reading Boolean values

Since Kotlin 1.4, you can read a Boolean value like this:

val b: Boolean = readLine().toBoolean()

and since Kotlin 1.6, you can read a Boolean value like this:

val b: Boolean = readln().toBoolean()

toBoolean() returns true if the input is "true", case insensitive. Otherwise, it will return false.

So, suppose you have the following input:

true
True
TRuE
T
false

The output will be as follows:

println(readln().toBoolean()) // true
println(readln().toBoolean()) // true
println(readln().toBoolean()) // true
println(readln().toBoolean()) // false
println(readln().toBoolean()) // false

You can use readLine()!!.toBoolean() with the Kotlin compiler version 1.3 or older:

val b: Boolean = readLine()!!.toBoolean()

Since Kotlin 1.5, you can use other functions to convert String to Boolean. The function toBooleanStrict() returns true if the string is equal to the word "true", case sensitive, and false if the string is equal to "false"; otherwise, your program will crash with an java.lang.IllegalArgumentException. The function toBooleanStrictOrNull() returns true if the string is equal to the word "true", case sensitive, and false if the string is equal to "false"; otherwise, it returns a special value null. We will discuss it later.

println("true".toBooleanStrict())     // true
// println("True".toBooleanStrict())  // program crashes
println("false".toBooleanStrict())    // false
// println("faLse".toBooleanStrict()) // program crashes

println("true".toBooleanStrictOrNull())  // true
println("false".toBooleanStrictOrNull()) // false
println("faLse".toBooleanStrictOrNull()) // null

Logical operators

Boolean type variables construct logical statements with the help of logical operators. Kotlin has four logical operators: NOT, AND, OR, and XOR:

  • NOT is a unary operator that reverses the Boolean value. It can be denoted with !.

val f = false // f is false
val t = !f    // t is true
  • AND is a binary operator that returns true if both operands are true. Otherwise, it returns false. It can be denoted with &&.

val b1 = false && false // false
val b2 = false && true // false
val b3 = true && false // false
val b4 = true && true  // true 
  • OR is a binary operator that returns true if at least one operand is true. Otherwise, it returns false. It can be denoted with ||.

val b1 = false || false // false
val b2 = false || true  // true
val b3 = true || false  // true
val b4 = true || true   // true
  • XOR (exclusive OR) is a binary operator that returns true if the Boolean operands have different values. Otherwise, it is false.

val b1 = false xor false // false
val b2 = false xor true  // true
val b3 = true xor false  // true
val b4 = true xor true   // false

The XOR operator is not as popular as the other logical operators. But you can still use it, just in case.

Logical operator precedence

Take a look at the precedence of logical operations in Kotlin below. Precedence determines how other variables are grouped in the absence of parentheses:

  1. ! for NOT

  2. xor for XOR

  3. && for AND

  4. || for OR

So, the variable below is true:

val bool = true && !false // true because !false is evaluated first

You can use parentheses (...) to change the order of execution.

For example, let's write a Boolean expression that determines the possibility of going on a hike during the summer and in other seasons:

val cold = false
val dry = true
val summer = false // suppose it is autumn now

val hiking = dry && (!cold || summer) // true, let's go hiking!

Do not get carried away by the expression above. A good programmer should understand not only arithmetic rules but also logical operations. Otherwise, you can end up hiking in bad weather.

Conclusion

In this topic, we have discussed the four main Kotlin logical operators — ! (NOT), xor (exclusive OR), && (AND), and || (OR). You need to understand them well to master Kotlin. They also comply with a special order of their execution, called precedence. It may seem confusing at first, but practice makes perfect. Speaking of which, let's solve some tasks!

1637 learners liked this piece of theory. 30 didn't like it. What about you?
Report a typo