LAST UPDATED: AUGUST 10, 2020
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:
-
Hard keyword
-
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:
Hard keywords
as
|
break
|
class
|
continue
|
do
|
else
|
false
|
for
|
fun
|
if
|
in
|
interface
|
is
|
null
|
object
|
package
|
return
|
super
|
this
|
throw
|
true
|
try
|
typealias
|
typeof
|
val
|
var
|
when
|
while
|
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:
by
|
which
|
constructor
|
delegate
|
dynamic
|
field
|
file
|
finally
|
get
|
import
|
init
|
param
|
property
|
receiver
|
set
|
setparam
|
where
|
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
|
|
|
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.