Menu

Kotlin Keywords and Identifiers

Kotlin Keywords and Identifiers

Every programming language has certain predefined words which carries some special meaning. They are known as keywords. Keywords cannot be used as an identifier.

An identifier is the name given to a variable, class, function, interface etc.

For example, name is an identifier here:

val name: String = "emp_name"

However, in Kotlin we can use some of the keywords as an identifier depending upon their type.

Types of keywords

There are two types of keywords in Kotlin:

  1. Hard keyword
  2. Soft keyword

Hard Keywords

Hard keywords cannot be used as an identifier. An error will be thrown if they are used as an identifier.

For example, in the HelloWorld program we created a function using the fun keyword. If we try creating a variable with name fun, we'll get an error.

val fun = 5   // Error

The Hard keywords in Kotlin are:

as

break

class

continue
doelsefalsefor
funifininterface
isnullobjectpackage
returnsuperthisthrow
truetrytypealiastypeof
valvarwhenwhile

Soft Keywords

Soft keywords are treated as keywords in a certain context, whenever they are applicable. In other contexts, they can be used as an identifier. For example, private is considered as keyword when we are setting the visibility of a class or a member. In another context, it can be used as an identifier.

val private = 5 // No Error

The soft keywords in Kotlin are:

bywhichconstructordelegate
dynamicfieldfilefinally
getimportinitparam
propertyreceiversetsetparam
whereactualabstractannotation
companionconstcrossinlinedata
enumexpectexternalfinal
infixinlineinnerinternal
lateinitnoinlineopenoperator
outoverrideprivateprotected
publicreifiedsealedsuspend
tailrecvararg  

Summary

In this tutorial, we studied about keywords and identifiers. We will use and study most of the keywords in this tutorial series.

Next, we will study about variables in Kotlin.