Table of contents
1.
Introduction
2.
Kotlin Keywords
2.1.
Hard Keywords
2.2.
Soft Keywords
3.
Commonly used Keywords
3.1.
The ‘if’ and ‘else’ Keywords
3.2.
The ‘package’ and ‘import’ Keywords
3.3.
The ‘val’ and ‘var’ Keywords
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Kotlin Keywords

Author Vasu Bansal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Commonly used Keywords

In the following section, we will discuss some commonly used keywords in Kotlin and understand them with the help of examples.

The ‘if’ and ‘else’ Keywords

These keywords are used to write conditional logic. The following code snippet denotes the syntax for using these keywords.

Kotlin if:

if (condition) {
    /*
 If the condition written in parenthesis is true, this block of code will be executed.
*/
}

Note that the keyword needs to be in lowercase. Upper casing of the keyword will generate an error.

Kotlin else:

if (condition) {
    /*
 If the condition written in parenthesis is true, this block of code will be executed.
*/
} else {
    /*
 If the condition 
written in parenthesis is false, this block of code will be executed.
*/
}

The ‘package’ and ‘import’ Keywords

Packages are the building blocks of a Kotlin project. One or more Kotlin files are included in a package. Packages are specified at the top of the source file with the help of the package keyword. Source files can be placed arbitrarily in the file system and need not necessarily match the imported packages directories.

package my.demo

import kotlin.text.*

The ‘val’ and ‘var’ Keywords

The keyword var is used to declare mutable variables. It denotes that the value of this variable may change in the subsequent program. On the other hand, the keyword val is used to declare immutable variables. It denotes that the value of the variable declared using the val keyword cannot be changed after its declaration in the subsequent program. For example, the following code snippet denotes the usage of var and val keywords.
 

// Kotlin program to demonstrate variables declared using var keyword
fun main(){
    var book_count = 50
    println("Old Book Count: $book_count")
    // Meanwhile if the book count doubles !!!
    book_count *= 2
    println("New Book Count: $book_count")
}

Output:

Old Book Count: 50
New Book Count: 100

 

// Kotlin program to demonstrate variables declared using val keyword
fun main(){
    val pi = 3.14 // It is a constant value, so it must be declared using the val keyword
    pi = 3.41 // This will throw error
}

Output:

error: Val cannot be reassigned

For further reading about the val and var keywords, also read the blog Kotlin Variables on the Coding Ninjas website.

FAQs

  1. What is the role of the "actual" keyword in Kotlin?
    The keyword "actual" denotes a platform-specific implementation in multiplatform projects.
     
  2. Can we use Kotlin keywords as identifiers?
    Yes, we can use some of the keywords in Kotlin as an identifier depending upon their type (hard and soft keywords). 
     
  3. What is the use of the “out” keyword in Kotlin?
    The keyword "out" is used to mark a parameter as covariant.

Key Takeaways

Cheers if you reached here!! This blog aims to introduce you to the concept of keywords and identifiers in Kotlin. Go ahead and try using some of these keywords in your Kotlin programs.

Yet there is never an end to the learning process, so check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development and how to design the applications of future. Until then, good luck!!

Live masterclass