Introduction
Every programming language has a certain set of predefined words that carry some special meaning. Such special words are known as keywords. In this blog, you will learn about various keywords in Kotlin and also learn the relation between keywords and identifiers. The names we give to a variable, class, function, interface, etc., are known as an identifier.
Kotlin Keywords
Keywords are a reserved set of words with special meanings to the compiler. These words may or may not be used as an identifier depending upon the type of the keyword and the context where they are used. For example,
val marks = 100
Here, “val” is a keyword. It indicates that “marks” is a variable.
In the following section, we will see the different types of keywords available in Kotlin.
Hard Keywords
Hard keywords are keywords that cannot be used as an identifier. The compiler will throw an error if these keywords are used as identifiers.
For example, in the code snippet presented below, the compiler will throw an error as both “val” and “while” are keywords in Kotlin. Hence, we cannot declare a variable with the name “while” in Kotlin.
val while = 1 // This will result in error
The Hard keywords in Kotlin are:
fun |
if |
in |
interface |
true |
try |
typealias |
typeof |
do |
else |
false |
for |
return |
super |
this |
throw |
val |
var |
when |
while |
is |
null |
object |
package |
as |
break |
class |
continue |
For a detailed explanation of each of these keywords, refer to the official Kotlin documentation here.
Soft Keywords
Soft keywords are those words that are treated as keywords depending upon the context in which they are used.
For example, “private” is treated as a keyword when we are setting the visibility of a class or a member. But in some other context, it can be used as an identifier. The following code snippets denote the same.
// Using private as an identifier
val private = 5 // No Error
// Using private as a keyword
class Company {
private val employeee_name = "Ninja"
}
The soft keywords in Kotlin are:
| get | import | init | param |
| where | actual | abstract | annotation |
| infix | inline | inner | internal |
| enum | expect | external | final |
| by | which | constructor | delegate |
| lateinit | noinline | open | operator |
| public | reified | sealed | suspend |
| companion | const | crossinline | data |
| dynamic | field | file | finally |
| property | receiver | set | setparam |
| out | override | private | protected |
| tailrec | vararg |
For a detailed explanation of each of these keywords, refer to the official Kotlin documentation here.




