BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Kotlin Keywords, Soft Keywords and Identifiers

Last Updated: February 20, 2019 by Chaitanya Singh | Filed Under: Kotlin Tutorial

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.

Types of Keywords in Kotlin

We have two types of keywords:
1. Hard Keywords
2. Soft Keywords

1. Hard Keywords

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
    println(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 whitespaces.
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 practise 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, sumOfTwo.

Reference

Kotlin Docs – Keywords Reference

❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Hello World Program – First Kotlin Program
  2. Kotlin Nested and Inner Class with examples
  3. Kotlin Class and Objects – Object Oriented Programming (OOP)
  4. Kotlin Tutorial for Beginners | Learn Kotlin
  5. Kotlin Constructors with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap