Kotlin Basics
AlightYoung 10/31/2022 Kotlin
# Hello World
fun main() {
println("Hello world")
}
1
2
3
2
3
# Define/Declare Keyword
val
: define a variable that cannot be modifiedvar
: define a variable that can be modified
# Basic Data Type
# Numbers
# Integer types
Byte
: 8 bitsShort
: 16 bitsInt
: 32 bitsLong
: 64 bits
fun main() {
var a: Byte = 8
var b: Short = 16
var c = 32
var d: Long = 64
println(a::class.simpleName) // Byte
println(b::class.java.typeName) // short
println(c::class.simpleName) // Int
println(d::class.java.typeName) // long
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# Floating-point types
Float
: 32 bitsDouble
: 64 bits
fun main() {
var a = 32f
var b = 64.0
println(a::class.simpleName) // Float
println(b::class.simpleName) // Double
}
1
2
3
4
5
6
2
3
4
5
6
# Literal constants
fun main() {
var a = 0xff // hexadecimal == 255
var b = 0b11 // binary == 3
var c = 1e9 // 1 * 10 ^ 9 ==> 1000000000
var d = 1_000_000_000 // 1000000000
// Octal literals are not supported in Kotlin.
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# Unsigned Types
UByte
UShort
UInt
ULong
- ...
https://kotlinlang.org/docs/unsigned-integer-types.html (opens new window)
# Array
# Null Safety
# ?
nullable types
By default, Kotlin data type is non-null types, unless you declare it as nullable types with ?
.
fun main() {
var a: Int = 1 // cannot be null
var b: Int? = null // can be null
}
1
2
3
4
2
3
4
# Safe Calls
val a = "Kotlin"
val b: String? = null
println(b?.length)
println(a?.length) // Unnecessary safe call
// check null
val a: String? = null
println(if (a?.length == null) "a is null" else "a not null")
println(a?.length == null)
a?.length?.dec()?.inc() // call in chain, return null instead of throw NPE
// let: pass null in safe call
val listWithNulls: List<String?> = listOf("Kotlin", null)
for (item in listWithNulls) {
item?.let { println(it) } // prints Kotlin and ignores null
}
// safe call on left side of assignment
// If either `person` or `person.department` is null, the function is not called:
// person?.department?.head = managersPool.getManager()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Elvis Operator
${nullable variable} ?: ${otherwise value}
:
i.e.
val l = b?.length ?: -1
it equals to:
val l: Int = if (b != null) b.length else -1
Since throw
and return
are expressions in Kotlin, they can also be used on the right-hand side of the Elvis operator.
fun foo(node: Node): String? {
val parent = node.getParent() ?: return null
val name = node.getName() ?: throw IllegalArgumentException("name expected")
// ...
}
1
2
3
4
5
2
3
4
5
# Not Null Assertion Operator !!
!!
: Convert any value to non-null type, and will throw NPE if the reference is null.
i.e. val l = b!!.length
# Safe Cast
val aInt: Int? = a as? Int
: Return null instead of throwing ClassCastException
when casting objects fail.
# Collections of a nullable type
val nullableList: List<Int?> = listOf(1, 2, null, 4)
val intList: List<Int> = nullableList.filterNotNull()
1
2
2
null-safety detail: https://kotlinlang.org/docs/null-safety.html (opens new window)
# Collections
todo...