Sling Academy
Home/Kotlin/How to Use `true` and `false` in Kotlin

How to Use `true` and `false` in Kotlin

Last updated: November 29, 2024

Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM) and it is designed to be fully interoperable with Java. One of the basic concepts in Kotlin, as in many other programming languages, is the use of true and false to represent Boolean values.

Understanding Booleans

In Kotlin, the Boolean type has two possible values: true and false. They are used to perform logical operations and control flow in your programs.

Declaration and Initialization

You declare a Boolean variable using the var or val keyword, followed by the variable name and datatype Boolean. You then assign either true or false to the variable.

var isKotlinFun: Boolean = true
val isJavaFun: Boolean = false

Using Booleans in Control Flow

Boolean values are often used in control flow structures such as if statements and when expressions to direct the program’s execution flow.

Using Booleans with if Statements

val isRaining = true

if (isRaining) {
    println("Take an umbrella!")
} else {
    println("Enjoy the sun!")
}

In this example, the code checks the Boolean value of isRaining. If it’s true, it prints "Take an umbrella!" otherwise, it prints "Enjoy the sun!".

Using Booleans with when Expressions

val isWeekend = false

when (isWeekend) {
    true -> println("Relax, it's the weekend!")
    false -> println("Time to work!")
}

The when expression is also suitable for handling Boolean values. In this example, the action taken depends on whether isWeekend is true or false.

Logical Operators

Kotlin provides several logical operators that you can use to work with Boolean values:

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)

Logical AND (&&)

val hasSchool = true
val isVacation = false

if (hasSchool && !isVacation) {
    println("Go to school.")
}

In this example, both conditions must be true for the body of the if statement to execute.

Logical OR (||)

val isWeekend = true
val isHoliday = false

if (isWeekend || isHoliday) {
    println("You can relax!")
}

Here, the message is printed if either isWeekend or isHoliday is true.

Logical NOT (!)

val isQuiet = false

if (!isQuiet) {
    println("It's loud here!")
}

The ! operator inverts the Boolean value of isQuiet. So, if isQuiet is false, !isQuiet would be true.

Summary

Understanding how to use true and false in Kotlin is fundamental. These Boolean values allow you to control the flow of your program, making decisions based on complex conditions, and handling different scenarios effectively.

Next Article: Kotlin - Working with Logical Operators: AND, OR, and NOT

Previous Article: Introduction to Booleans in Kotlin

Series: Primitive data types in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin