Xóa bài viết
Bạn có chắc chắn muốn xóa bài viết này không ?
Xóa bình luận
Bạn có chắc chắn muốn xóa bình luận này không ?
Kotlin Cheat Sheet - Basics - 1
Basic Types
- In Kotlin, everything is an object.
Numbers
- Types: Double, Float, Long, Int, Short, Byte
- Tags: Long (L), Float (f, F) -> 123.5f
- Underscores: 999_99_9999L -> readable
- Optional - Boxing = null || value
val a: Int = 10000
println(a === a) // Prints 'true'
val boxedA: Int? = a
val anotherBoxedA: Int? = a
println(boxedA === anotherBoxedA) // !!!Prints 'false'!!!
- Casting
val b: Byte = 1
val i: Int = b.toInt() // OK: explicitly widened
val l = 1L + 3 // Long + Int => Long
Arrays
- Arrays in Kotlin are immutable.
- Use [ ] / get(), set() method
val numbers: IntArray = intArrayOf(10, 20, 30, 40, 50)
/* Creates an Array<String>
with values ["0", "1", "4", "9", "16"] */
val asc = Array(5) { i -> (i * i).toString() }
asc.forEach { println(it) }
val x: IntArray = intArrayOf(1, 2, 3)
x[0] = x[1] + x[2]
Strings
Strings are immutable.
- Raw string
val text = """
for (c in "foo")
print(c)
"""
- String Templates
val i = 10
println("i = $i") /* prints "i = 10" */
val s = "abc"
println("$s.length is ${s.length}") /* prints "abc.length is 3" */
Packages
package foo.bar
fun baz() {...}
class Goo {...}
Imports
- Single:
import foo.Bar
- Multiple
import foo.*
/*everything (package, class, object etc)
in 'foo' becomes accessible*/
- Namesake
import foo.Bar
/* Bar is accessible*/
import bar.Bar as bBar
/* bBar stands for 'bar.Bar'*/
Control Flow
- Don't have ternary operator (condition ? then : else), use if else instead.
// As expression
val max = if (a > b) a else b
val max = if (a > b) {
print("Choose a")
a
} else {
print("Choose b")
b
}
- Don't have Switch
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}
when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}
fun hasPrefix(x: Any) = when(x) { /* is | !is */
is String -> x.startsWith("prefix")
else -> false
}
when { /* replace if-else if chain */
x.isOdd() -> print("x is odd")
x.isEven() -> print("x is even")
else -> print("x is funny")
}
fun Request.getBody() =
when (val response = executeRequest()) {
is Success -> response.body
is HttpError -> throw HttpException(response.status)
}
- For Loops
for (item in collection) print(item)
for (i in 1..3) {
println(i)
}
for (i in 6 downTo 0 step 2) {
println(i)
}
for (i in array.indices) {
println(array[i])
}
for ((index, value) in array.withIndex()) {
println("the element at $index is $value")
}
- while and do..while work as usual
Bình luận

{{ comment.user.name }}
Bỏ hay
Hay

Cùng một tác giả

3
0
Giới thiệu Struct là một kiểu dữ liệu, nó cho phép chúng ta kết hợp các dữ liệu khác kiểu và/hoặc cùng kiểu nhau để tạo thành một kiểu dữ liệu mớ...

1
0
Setup Cài đặt Meteor OSX/Linux curl https://install.meteor.com/ | sh Window https://install.meteor.com/windows Khởi tạo project Meteor meteor ...

1
1
TNgày 8/12/2018 Bài này m viết lưu trữ để sau này cài lại đỡ phải mò. Cầu hình máy đã cài: CPU Intel Core i5 8600K 3.6Ghz Turbo Up to 4.3Ghz / 9...
Bài viết liên quan

0
5
fCC: Technical Documentation Page note So I have finished the HTML part of this exercise and I want to come here to lament about the lengthy HTML ...

4
0
I used Spring boot, Hibernate few times back then at University, I'v started using it again recently. In this (Link), I want to check how Spring J...

24
1
Toán tử XOR có tính chất: + A XOR A = 0 + 0 XOR A = A Với tính chất này, có thể cài đặt bài toán sau với độ phức tạp O(N) về runtime, và với O(1)...