1. Hard Keyword
As
break
for (j in 1..100) {
if (...) break
class
}
continue
for (j in 1..100) {
if (...) continue
do
do {
val y = retrieveData()
} while (y != null)
else
fun
for
Fungsi : loop
// ...
if
var max = a
if (a < b) max = b
in
print(i)
}
interface
interface MyInterface {
fun bar()
fun foo() {
// optional body
is
if (obj is String) {
print(obj.length)
null
Fungsi : konstanta yang mewakili referensi objek yang tidak
menunjuk ke objek apapun?
Object
window.addMouseListener(object : MouseAdapter() {
})
Package
package org.example
Return
fun foo() {
listOf(1, 2, 3, 4, 5).forEach {
print(it)
}
Super
This
class Person {
var children: MutableList<Person> = mutableListOf<>()
constructor(parent: Person) {
parent.children.add(this)
}
}
Throw
True
Try
try {
// meyimpan kode yang membangkitkan exception
}
Typeallias
Typeof
Fungsi : idk
Val
Var
When
While
while (x > 0) {
x--
}
2. Soft Keywords
By
Fungsi : mewakilkan interface dengan memakai object
interface Base {
fun print()
}
class BaseImpl(val x: Int) : Base {
override fun print() { print(x) }
}
class Derived(b: Base) : Base by b
fun main() {
val b = BaseImpl(10)
Derived(b).print()
}
Catch
try {
// some code
}
catch (e: SomeException) {
// handler
}
finally {
// optional finally block
}
Constructor
Fungsi : ???
Dynamic
Field
Fungsi :
When you're annotating a property or a primary constructor
parameter, there are multiple Java elements which are
generated from the corresponding Kotlin element, and
therefore multiple possible locations for the annotation in
the generated Java bytecode. To specify how exactly the
annotation should be generated, use the following syntax:
File
@file:JvmName("Foo")
package org.jetbrains.demo
Finally
try {
// some code
}
catch (e: SomeException) {
// handler
}
finally {
// optional finally block
}
Get
[<getter>]
[<setter>]
Examples:
get() = this.size == 0
Import
Init
init {
println("First initializer block that prints ${name}")
init {
Param
Fungsi :
Property
Fungsi :
property (annotations with this target are not visible to
Java);
If you don't specify a use-site target, the target is chosen
according to the @Target annotation of the annotation
being used. If there are multiple applicable targets, the first
applicable target from the following list is used:
param;
property;
field.
Receiver
Fungsi :
receiver (receiver parameter of an extension function or
property);
To annotate the receiver parameter of an extension
function, use the following syntax:
Set
get() = this.toString()
set(value) {
Setparam
Fungsi :
setparam (property setter parameter);
Where
Fungsi :
If the same type parameter needs more than one upper
bound, we need a separate where-clause:
where T : CharSequence,
T : Comparable<T> {
+, -, *, /, %
=
Fungsi = assignment operator, can be used to specify default
values for parameter
Function parameters can have default values, which are
used when a corresponding argument is omitted. This
allows for a reduced number of overloads compared to
other languages:
Fungsi :
++, --
&&, ||, !
Fungsi :
===, !==
Fungsi :
Referential equality is checked by the === operation (and
its negated counterpart !==). a === b evaluates to true if
and only if a and b point to the same object. For values
which are represented as primitive types at runtime (for
example, Int), the === equality check is equivalent to
the == check.
[, ]
Fungsi :
Square brackets are translated to calls to get and set with
appropriate numbers of arguments.
!!
Fungsi :
val l = b!!.length
Thus, if you want an NPE, you can have it, but you have to
ask for it explicitly, and it does not appear out of the blue.
?.
val a = "Kotlin"
println(b?.length)
?:
val l = b?.length ?: -1
::
Fungsi : creates a member reference or a class reference
println(numbers.filter(::isOdd))
..
print(i)
println()
Hasil :
1357
8642
print(i)
Hasil :
123456789
:
?
Fungsi : marks a type as nullable
In Kotlin, the type system distinguishes between references
that can hold null (nullable references) and those that can
not (non-null references). For example, a regular variable
of type String can not hold null:
b = null // ok
print(b)
->
@
for (j in 1..100) {
if (...) break@loop
;
$
val i = 10
_