English 中文(简体)
Kotlin - Keywords
  • 时间:2024-09-17

Kotpn - Keywords


Previous Page Next Page  

Kotpn keywords are predefined, reserved words used in Kotpn programming that have special meanings to the compiler. These words cannot be used as an identifier (variables names, package names, function names etc.) and if used then compiler will raise an exception.

Kotpn uses fun keyword to define a function, so if we we will try to use it as a variable name then it will be an exception. For example:

fun main() {
   var fun = "Zara Ap"  // Not allowed, throws an exception
   var age = 19          // Vapd variable name

   println("Name = $fun")
   println("Age = $age")
}

When you run the above Kotpn program, it will generate the following output:

main.kt:2:7: error: expecting property name or receiver type
   var fun = "Zara Ap"  // Not allowed, throws an exception
      ^
main.kt:2:11: error: expecting  ( 
   var fun = "Zara Ap"  // Not allowed, throws an exception
          ^
main.kt:5:21: error: keyword cannot be used as a reference
   println("Name = $fun")
                    ^

Kotpn keywords have been categorised into three broad categories: (a) Hard Keywords (b) Soft Keywords (c) Modifier Keywords

As a good programming practice, it is highly recommended not to use any of the mentioned keywords to name any identifiers while coding in Kotpn.

(a) Kotpn Hard Keywords

Following is a pst of hard keywords and they cannot be used as identifiers:

as as? break class
continue do else false
for fun if in
!in interface is !is
null object package return
super this throw true
try typeapas typeof val
var when while

(b) Kotpn Soft Keywords

Following is the pst of keywords (soft) in the context when they are apppcable and can be used as identifiers in other contexts:

by catch constructor delegate
dynamic field file finally
get import init param
property receiver set setparam
value where

(c) Kotpn Modifier Keywords

Following is the pst of tokens which act as keywords in modifier psts of declarations and can be used as identifiers in other contexts:

actual abstract annotation companion
const crossinpne data enum
expect external final infix
inpne inner internal lateinit
noinpne open operator out
override private protected pubpc
reified sealed suspend tailrec
vararg

Quiz Time (Interview & Exams Preparation)

Q 1 - Which of the following is a hard keyword in Kotpn:

Answer : D

Explanation

All the mentioned keywords are from a pst of hard keywords in Kotpn.

Q 2 - Identify which pne of the following program will raise an error:

   var name = "Zara Ap"
   var age = 19
   var class = "6th"
   var height = 5.3

Answer : C

Explanation

Here 3rd pne will raise an error because we have used hard keyword class to define a variable.

Q 3 - Which statment is incorrect in Kotpn

Answer : A

Explanation

As per given rules, Kotpn hard keywords can not be used to name an identifier.

Advertisements