Keywords and Operators
Hard Keywords
The following tokens are always interpreted as keywords and cannot be used as identifiers:
as
- is used for type casts
- specifies an alias for an import
as?
is used for safe type castsbreak
terminates the execution of a loopclass
declares a classcontinue
proceeds to the next step of the nearest enclosing loopdo
begins a do/while loop (loop with postcondition)else
defines the branch of an if expression which is executed when the condition is falsefalse
specifies the ‘false’ value of the Boolean typefor
begins a for loopfun
declares a functionif
begins an if expressionin
- specifies the object being iterated in a for loop
- is used as an infix operator to check that a value belongs to a range, a collection or another entity that defines the ‘contains’ method
- is used in when expressions for the same purpose
- marks a type parameter as contravariant
!in
- is used as an operator to check that a value does NOT belong to a range, a collection or another entity that defines the ‘contains’ method
- is used in when expressions for the same purpose
interface
declares an interfaceis
- checks that a value has a certain type
- is used in when expressions for the same purpose
!is
- checks that a value does NOT have a certain type
- is used in when expressions for the same purpose
null
is a constant representing an object reference that doesn’t point to any objectobject
declares a class and its instance at the same timepackage
specifies the package for the current filereturn
returns from the nearest enclosing function or anonymous functionsuper
this
throw
throws an exceptiontrue
specifies the ‘true’ value of the Boolean typetry
begins an exception handling blocktypealias
declares a type aliastypeof
reserved for future useval
declares a read-only property or local variablevar
declares a mutable property or local variablewhen
begins a when expression (executes one of the given branches)while
begins a while loop (loop with precondition)
Soft Keywords
The following tokens act as keywords in the context when they are applicable and can be used as identifiers in other contexts:
by
catch
begins a block that handles a specific exception typeconstructor
declares a primary or secondary constructordelegate
is used as an annotation use-site targetdynamic
references a dynamic type in Kotlin/JS codefield
is used as an annotation use-site targetfile
is used as an annotation use-site targetfinally
begins a block that is always executed when a try block exitsget
- declares the getter of a property
- is used as an annotation use-site target
import
imports a declaration from another package into the current fileinit
begins an initializer blockparam
is used as an annotation use-site targetproperty
is used as an annotation use-site targetreceiver
is used as an annotation use-site targetset
- declares the setter of a property
- is used as an annotation use-site target
setparam
is used as an annotation use-site targetwhere
specifies constraints for a generic type parameter
Modifier Keywords
The following tokens act as keywords in modifier lists of declarations and can be used as identifiers in other contexts:
actual
denotes a platform-specific implementation in multiplatform projectsabstract
marks a class or member as abstractannotation
declares an annotation classcompanion
declares a companion objectconst
marks a property as a compile-time constantcrossinline
forbids non-local returns in a lambda passed to an inline functiondata
instructs the compiler to generate canonical members for a classenum
declares an enumerationexpect
marks a declaration as platform-specific, expecting an implementation in platform modules.external
marks a declaration as implemented not in Kotlin (accessible through JNI or in JavaScript)final
forbids overriding a memberinfix
allows calling a function in infix notationinline
tells the compiler to inline the function and the lambdas passed to it at the call siteinner
allows referring to the outer class instance from a nested classinternal
marks a declaration as visible in the current modulelateinit
allows initializing a non-null property outside of a constructornoinline
turns off inlining of a lambda passed to an inline functionopen
allows subclassing a class or overriding a memberoperator
marks a function as overloading an operator or implementing a conventionout
marks a type parameter as covariantoverride
marks a member as an override of a superclass memberprivate
marks a declaration as visible in the current class or fileprotected
marks a declaration as visible in the current class and its subclassespublic
marks a declaration as visible anywherereified
marks a type parameter of an inline function as accessible at runtimesealed
declares a sealed class (a class with restricted subclassing)suspend
marks a function or lambda as suspending (usable as a coroutine)tailrec
marks a function as tail-recursive (allowing the compiler to replace recursion with iteration)vararg
allows passing a variable number of arguments for a parameter
Special Identifiers
The following identifiers are defined by the compiler in specific contexts and can be used as regular identifiers in other contexts:
field
is used inside a property accessor to refer to the backing field of the propertyit
is used inside a lambda to refer to its parameter implicitly
Operators and Special Symbols
Kotlin supports the following operators and special symbols:
+
,-
,*
,/
,%
- mathematical operators*
is also used to pass an array to a vararg parameter
=
- assignment operator
- is used to specify default values for parameters
+=
,-=
,*=
,/=
,%=
- augmented assignment operators++
,--
- increment and decrement operators&&
,||
,!
- logical ‘and’, ‘or’, ‘not’ operators (for bitwise operations, use corresponding infix functions)==
,!=
- equality operators (translated to calls ofequals()
for non-primitive types)===
,!==
- referential equality operators<
,>
,<=
,>=
- comparison operators (translated to calls ofcompareTo()
for non-primitive types)[
,]
- indexed access operator (translated to calls ofget
andset
)!!
asserts that an expression is non-null?.
performs a safe call (calls a method or accesses a property if the receiver is non-null)?:
takes the right-hand value if the left-hand value is null (the elvis operator)::
creates a member reference or a class reference..
creates a range:
separates a name from a type in declarations?
marks a type as nullable->
- separates the parameters and body of a lambda expression
- separates the parameters and return type declaration in a function type
- separates the condition and body of a when expression branch
@
- introduces an annotation
- introduces or references a loop label
- introduces or references a lambda label
- references a ‘this’ expression from an outer scope
- references an outer superclass
;
separates multiple statements on the same line$
references a variable or expression in a string template_
- substitutes an unused parameter in a lambda expression
- substitutes an unused parameter in a destructuring declaration
当前内容版权归 kotlinlang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 kotlinlang .