Sling Academy
Home/Kotlin/Reserved Words in Kotlin: What You Can’t Use

Reserved Words in Kotlin: What You Can’t Use

Last updated: November 29, 2024

Kotlin, like many other programming languages, has a set of reserved words. These are keywords that have special meaning and purpose in the language syntax. When writing code, you cannot use these reserved words as variable names, function names, or any other identifiers. This article will guide you through these keywords and provide some examples to illustrate their usage.

What are Reserved Words?

Reserved words, also known as keywords, are pre-defined identifiers that are part of the language syntax. They tell the compiler about the structure and operations allowed in the program. They are integral to writing functions, loops, and various other constructs.

Common Reserved Words in Kotlin

Kotlin keywords can largely be divided into three categories:

  • Statements
  • Types
  • Control Flow

Statements

  • package
  • import
  • class
  • fun
  • interface

Types

  • Int, String, Boolean, etc.

Control Flow

  • if
  • else
  • while
  • return

Examples of Using Reserved Words

Let’s look at a simple code example with some common reserved words:


fun main() {
    val name: String = "Kotlin"
    if (name.isNotEmpty()) {
        println("Hello, $name!")
    } else {
        println("Hello, world!")
    }
}

In the code above:

  • fun - Defines a function in Kotlin.
  • val and String - Define immutable variables and types.
  • if and else - Used for control flow.

Why Can’t You Use Reserved Words?

Reserved words have predefined purposes in the language and determine the structure and control flow of programs. Using these words as variable names could lead to ambiguity and compilation errors.

For instance:


// Will cause a compilation error
val class: String = "User"

The code above will fail because class is a keyword that is reserved for declaring a class, not assigning a value.

Identifying Reserved Words

If you need to use a name that coincides with a reserved word, prefixes like backticks (`name`) can be utilized in Kotlin:


fun main() {
    val `class` = "Not a Kotlin class"
    println(`class`)
}

In this example, you 'trick' the compiler into accepting the reserved word as an identifier.

Next Article: Exploring Kotlin’s Keywords: The Essentials

Previous Article: Organizing Kotlin Code with Packages

Series: Basics of 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