
Keywords in Kotlin
📌 Keywords in Kotlin
Keywords in Kotlin are reserved words that have a specific meaning in the language. They are used to define structures like classes, functions, variables, and control flow statements.
You cannot use these keywords as variable names, class names, or function names unless you escape them using backticks (`keyword`
).
✅ 1. Categories of Kotlin Keywords
Kotlin keywords are categorized into the following groups:
Control Flow Keywords
Declaration Keywords
Modifier Keywords
Exception Handling Keywords
Object-Oriented Keywords
Type Keywords
Special Keywords
✅ 2. Control Flow Keywords
These keywords are used to control the flow of execution in a Kotlin program.
Keyword | Description |
---|---|
if | Conditional statement |
else | Executes if if condition is false |
when | Alternative to switch-case |
for | Looping statement |
while | Loop that executes until condition is false |
do | Executes a block at least once |
break | Terminates a loop |
continue | Skips the current iteration |
return | Returns from a function |
✅ 3. Declaration Keywords
These keywords are used for declaring variables, functions, and classes.
Keyword | Description |
---|---|
val | Immutable variable (Read-only) |
var | Mutable variable (Read-Write) |
fun | Declare a function |
class | Declare a class |
interface | Declare an interface |
object | Declare an object (Singleton) |
constructor | Define a constructor |
init | Initialization block |
this | Refers to the current object |
super | Refers to the parent class |
✅ 4. Modifier Keywords
Modifiers are used to specify the visibility and behavior of classes, functions, and properties.
Keyword | Description |
---|---|
public | Accessible from anywhere |
private | Accessible within the class only |
protected | Accessible within class and subclass |
internal | Accessible within the same module |
abstract | Define an abstract class or function |
open | Allow a class or function to be inherited or overridden |
final | Prevent overriding or inheritance |
override | Override a superclass method |
sealed | Restrict class inheritance |
data | Declare a data class |
inline | Inline functions to optimize performance |
lateinit | Declare variables without initializing them immediately |
const | Declare compile-time constants |
✅ 5. Exception Handling Keywords
These keywords handle exceptions and errors in Kotlin.
Keyword | Description |
---|---|
try | Block of code to test for exceptions |
catch | Handle exceptions |
finally | Block that executes regardless of exception |
throw | Throw an exception manually |
✅ 6. Object-Oriented Keywords
Kotlin provides several object-oriented programming keywords.
Keyword | Description |
---|---|
super | Access parent class methods or properties |
this | Reference the current object |
override | Override parent class methods |
abstract | Declare abstract classes or methods |
interface | Declare an interface |
implements | Implement an interface |
✅ 7. Type Keywords
These keywords represent different data types in Kotlin.
Keyword | Description |
---|---|
Int | Integer type |
Double | Floating-point type |
Float | Floating-point type |
Long | Large integer type |
Short | Smaller integer type |
Byte | 8-bit integer type |
Boolean | True/False value |
Char | Single character type |
String | Sequence of characters |
Unit | No return value |
Nothing | Represents "no value" |
Array | Collection of elements |
✅ 8. Special Keywords
These are some additional keywords with specific uses.
Keyword | Description |
---|---|
is | Type checking operator |
as | Type casting operator |
in | Check if value exists in a range or collection |
out | Covariance in generics |
in (Generics) | Contravariance in generics |
typealias | Provide an alias for a type |
by | Delegate properties or methods |
✅ 9. Conclusion
Control Flow Keywords manage the program's execution flow.
Declaration Keywords define variables, classes, and functions.
Modifier Keywords control visibility and behavior.
Exception Handling Keywords handle errors gracefully.
Object-Oriented Keywords support inheritance and abstraction.
Type Keywords provide data type declarations.
Special Keywords offer additional functionality.