Kotlin Language Features Map
Kotlin Language Features Map
Code examples
The main function
Hello World!
fun main() {
val name = "World"
println("Hello, $name!")
}
for (n in 1≤..≤100) {
when {
n % (3 * 5) == 0 -> println("FizzBuzz")
n % 3 == 0 -> println("Fizz")
n % 5 == 0 -> println("Buzz")
else -> println(n)
}
}
Smart casts
Code examples
Nullable types annotated with ‘?’ can be assigned to null value
You have to check the value of nullable type before accessing its properties
Safe cast with as? operator returns null on class cast failure
}
Functions & Lambdas Basics Intermediate Advanced
3/8
Code examples
Lambdas and functions
fun calculate(x: Int, y: Int, operation: (Int, Int) -> Int): Int {
return operation(x, y)
}
fun main() {
fun add(x: Int, y: Int) = x + y
println("sum=$sum, mul=$mul")
}
Object-oriented programming. Part 1 Basics Intermediate Advanced
4/8
Code examples
Implementing interfaces
interface Shape {
fun draw()
fun area(): Double
}
Code examples
The main purpose for data classes is to hold data
class User {
var name: String by Delegates.observable(initialValue:"N/A") {
property, old, new -> println("$old -> $new")
}
}
users.filter { it.name.startsWith(prefix"J") }
users.map { it.name }
users.sortedBy { it.name.last() }
users.sortedByDescending { it.age }
users.maxBy { it.age }
users.groupBy { it.name.first() }
Code examples
public class JavaClass {
String id;
String desc;