Kotlin Syntax & Language Basics
Estimated reading: 4 minutes 28 views

🧩 Kotlin – Keywords: Reserved Words in the Kotlin Language

🧲 Introduction – Why Learn Kotlin Keywords?

Keywords are the building blocks of Kotlin syntax. These are reserved words that have a predefined meaning in the Kotlin language and cannot be used as variable, class, or function names. Understanding Kotlin keywords is crucial for writing valid and expressive code while avoiding syntax errors.

🎯 In this guide, you’ll learn:

  • What Kotlin keywords are and why they are reserved
  • A full list of Kotlin reserved and soft keywords
  • How to escape keywords for use as identifiers
  • Best practices and common errors to avoid

🗂️ What Are Keywords in Kotlin?

Kotlin keywords are predefined words that the compiler interprets as commands or declarations. You cannot use them to name your variables, functions, classes, or any identifiers unless you escape them using backticks.

🧠 Example – Invalid:

val class = "Math" // ❌ Error: 'class' is a keyword

✅ Escaped Version:

val `class` = "Math"
println(`class`)

🧾 List of Kotlin Keywords

✅ Hard Keywords (Always Reserved)

Keywords
as as? break class continue do else false for fun
if in !in interface is !is null object package return
super this throw true try typealias typeof val var when
while by catch constructor delegate dynamic field file
finally get import init param property receiver set
setparam where

These are strictly reserved and cannot be redefined under any circumstances.


🧪 Soft Keywords (Contextual)

Soft keywords are only reserved in specific contexts, such as type declarations or DSLs.

Soft Keywords
actual abstract annotation companion const crossinline
data enum expect external final infix inline
inner internal lateinit noinline open operator
out override private protected public reified
sealed suspend tailrec vararg value inline

💡 These can still be used in normal contexts when wrapped with backticks.


🏷️ Special Identifier Escaping with Backticks

If you must use a keyword as a variable or function name (e.g., interfacing with Java code), escape it with backticks:

val `object` = "Escaped Keyword"
fun `fun`(): String = "This is valid"

✅ This is useful for:

  • Integrating with external APIs (e.g., Java method named object)
  • Writing readable DSLs
  • Handling conflicting names in legacy systems

✍️ Example – Using Keywords in Code

fun main() {
    val `return` = "Success"
    val `package` = "com.example"

    println("Status: $`return`")
    println("Package: $`package`")
}

✅ Output:

Status: Success
Package: com.example

📋 Kotlin Keywords Table Summary

TypeDescriptionExamples
🔒 HardAlways reservedfun, class, return, if
🔁 SoftReserved in specific contexts onlydata, suspend, sealed
✏️ EscapedBackticks allow keyword reuseval `class` = "A"

🚫 Common Mistakes

❌ Mistake✅ Fix
Using return as variableUse backticks: val `return`
Forgetting keyword contextAvoid using keywords for naming
Using keyword in functionEscape with backticks if needed

📌 Summary – Recap & Next Steps

Kotlin’s keyword system ensures code clarity, structure, and consistency. Knowing which words are reserved, contextual, or escapable helps you avoid syntax errors and write better, more maintainable Kotlin code.

🔍 Key Takeaways:

  • Kotlin keywords are reserved words used by the compiler.
  • Hard keywords are always reserved; soft keywords depend on context.
  • You can use backticks to escape keywords for use in identifiers.

⚙️ Practical Use:
When integrating Kotlin with legacy systems or Java codebases, escaped keywords allow safe reuse of method or field names that might otherwise cause conflicts.


❓ FAQs – Kotlin Keywords

Can I use class or object as variable names in Kotlin?
✅ Yes, but you must use backticks:

val `class` = "Math"
val `object` = "Instance"

What is the difference between hard and soft keywords?
✅ Hard keywords are always reserved; soft keywords are only reserved in specific contexts like modifiers, DSLs, or annotations.


Why does Kotlin allow backtick escaping?
✅ Escaping keywords with backticks helps with interoperability, especially when working with Java methods or APIs that use conflicting names.


Are Kotlin keywords case-sensitive?
✅ Yes. Kotlin is case-sensitive. For example, If is not the same as if.


How many keywords does Kotlin have?
✅ Kotlin has over 50+ reserved keywords, categorized into hard and soft types, along with support for escaped identifiers using backticks.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Kotlin – Keywords

Or Copy Link

CONTENTS
Scroll to Top