Home » Blog » Kotlin Basics – Kotlin Keywords

Kotlin Basics – Kotlin Keywords

[vc_row][vc_column][vc_column_text]There are certain words in Kotlin that have special meaning and cannot be used as identifiers(variable name, function name, class name etc). These words are called reserved words or keywords. In this guide, we will learn about keywords and identifiers.[/vc_column_text][vc_custom_heading text=”Types of Keywords in Kotlin
“][vc_column_text]We have two types of keywords:
1. Hard Keywords
2. Soft Keywords[/vc_column_text][vc_custom_heading text=”1. Hard Keywords”][vc_column_text]These keywords cannot be used as identifiers. For example
This is valid:

//valid variable name
val myvar = 5

This is invalid:

//error: "else" cannot be used as a variable name
val else = 5
Kotlin Hard keywords Table
as class break continue do else
for fun false if in interface
super return object package null is
try throw true this typeof typealias
when while val var

2. Soft Keywords

Soft keywords are the keywords that are used only in a certain context which means we can use them as identifier. Apart from the above list of keywords, there are other keywords that can be used as identifiers. For example, “by” is a soft keyword which delegates the implementation of an interface to another object. We can use the keyword “by” as identifier as well.


//valid code
fun main(args: Array) {
    val by=10

There are several other soft keywords available in Kotlin such as catch, get, finally, field etc.

Kotlin Identifiers

The name that we give to a variable, class, function etc is known as identifier. For example:

var num = 100

Here num is an identifier.

Naming convention of Kotlin Identifiers

1. The identifier cannot have white spaces.
2. Identifiers in Kotlin are case sensitive.
3. They cannot contain special characters such as @, #, % etc.
4. An identifier can start with an underscore “_”.
5. It is a best practice to give meaningful names to the identifiers. For example: add, multiply and divide are the meaningful identifier than the a, m and d.
6. If you wish to include two words in an identifier than you can start the second word with a capital letter. For example, sum Of Two.

Leave a Reply

Your email address will not be published.