Kotlin is a statically typed language inspired by Java, Scala, C#, and Groovy. It was created by JetBrains in 2010 and can target the JVM, JavaScript, and native platforms. Kotlin code is safe, approachable, and pragmatic. It follows Java conventions and is interoperable with Java libraries and packages. Kotlin supports features like classes, functions, control flow statements, null safety, and can be built with tools like Maven and Gradle.
5. What is Kotlin
• Statically typed language
• Inspired by Java Programming Language, Scala, C#, Groovy amongst others
• Targets
• JVM / Android
• JavaScript
• Native
6. Type of Applications
• Industrial Applications
• Mobile
• Client-Side
• Web
• Desktop
• Server-Side
7. Benefits while adopting
• Familiarity with C#, Java Programming Language, JavaScript, et al. allows for low ramp-up time
• Interoperable with the platform
• Java Programming Language - JVM
• JavaScript - Packages and Libraries
• Native - C Interop
8. Conventions
• Follows Java Programming Language Coding Conventions
• Types in Uppercase
• Methods and Properties in lower camelCase
• Semicolons optional*
• Packages follows reverse notation
• Multiple classes per file allowed
• Package does not have to match folder names
9. Tooling
• JDK
• Version 6, 7, 8 and 9
• Kotlin Compiler
• Editor or IDE
• IntelliJ IDEA, Android Studio, NetBeans, Eclipse
12. Variable
Immutable
val a: Int = 1
val b = 2 //inferred
val c: Int
c = 3
val name: String = "Daenarys"
val age = 25
val isTargaryen: Boolean = true
val armies = 1_000_000
Mutable
var a: Int = 1
var b = 2 //inferred
var c: Int
c = 3
c = 5
var name: String = "Daenarys"
var age = 25
var isTargaryen: Boolean = true
var armies = 1_000_000
14. Control Flow
IF ELSE
val nilai = 80
if (nilai > 60) {
println("Lulus")
} else {
println("Tidak Lulus")
}
SHORT IF ELSE
val nilai = 80
val hasil = if (nilai > 60) "Lulus"
else "Tidak Lulus"
println(hasil)
15. Control Flow
FOR LOOPS
for (i in 1..10) {
println(i)
}
FOR LOOPS COLLECTIONS
var list = listOf(1,2,3,4,5)
for (i in list) {
println(i)
}
16. Class
class Persegi(var sisi: Int)
{
fun getKeliling(): Int {
return 4 * sisi
}
fun getLuas(): Int {
return sisi * sisi
}
}
fun main(args: Array<String>) {
val p = Persegi(5)
println("Luas: ${p.getLuas()}
Keliling ${p.getKeliling()}")
}
17. Null Safety
fun main(args: Array<String>) {
var name: String? = "Jon Snow"
val length = name?.length
println(length)
}