Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DevCoach 187: Kotlin 101 | Kuasai Bahasa Kotlin...

Avatar for Zahrina Zahrina
March 17, 2025
67

DevCoach 187: Kotlin 101 | Kuasai Bahasa Kotlin dalam 1 Jam!

Sudah pernah dengar tentang Kotlin? Bahasa pemrograman modern ini dipilih langsung oleh Google sebagai bahasa utama untuk pengembangan aplikasi Android. Tapi tahukah kamu? Kotlin bukan hanya untuk Android—bahasa ini juga digunakan dalam web development, backend server, hingga aplikasi multi-platform!

Dengan sintaks yang lebih ringkas, ekspresif, dan powerful, Kotlin menjadi pilihan banyak developer. 🚀
Ingin belajar Kotlin dari nol dan langsung praktek? Yuk, gabung di sesi Dev Coach ini! Dalam 1 jam, kita akan belajar dasar-dasarnya dan setup IDE untuk Kotlin

Avatar for Zahrina

Zahrina

March 17, 2025
Tweet

Transcript

  1. Ngobrolin apa kita kali ini? • Kotlin Fundamental • Sintaks

    Dasar Kotlin • Instalasi & Setup IDE untuk Kotlin Kotlin
  2. Kenapa Kotlin? • Concise code combined with greater readability •

    Interoperability with Java • Support for multiplatform development • Code safety • Easy learning • Big community
  3. Kenapa Kotlin? • Concise code combined with greater readability •

    Interoperability with Java • Support for multiplatform development • Code safety • Easy learning • Big community
  4. Kenapa Kotlin? • Concise code combined with greater readability •

    Interoperability with Java • Support for multiplatform development • Code safety • Easy learning • Big community
  5. Kenapa Kotlin? • Concise code combined with greater readability •

    Interoperability with Java • Support for multiplatform development • Code safety • Easy learning • Big community
  6. Kenapa Kotlin? • Concise code combined with greater readability •

    Interoperability with Java • Support for multiplatform development • Code safety • Easy learning • Big community
  7. Meet with “Hello World Kotlin!ˮ Hello Kotlin! merupakan sebuah program

    sederhana yang digunakan untuk mencetak sebuah teks “Hello Kotlin!ˮ ke dalam layar atau konsol. fun main() { println("Hello Kotlin!") println(“Hello Dicoding!ˮ) } Fungsi pertama yang akan dieksekusi. Fungsi untuk menampilkan teks ke dalam program.
  8. Variabel untuk menyimpan nilai var dan val digunakan untuk mengontrol

    nilai dari sebuah variabel. Dengan kata kunci var kita bisa mengubah nilai yang sudah kita inisialisasikan. val myAge = 20 myAge = 22 //Error : val cannot be reassigned var myAge = 20 myAge = 22 Nilainya dapat diubah (mutable) Nilainya tidak dapat diubah (read-only)
  9. Tipe Data Tipe data adalah klasifikasi untuk menentukan tipe dari

    nilai yang dimiliki variabel. Char Berisi satu karakter saja ‘Aʼ String Kumpulan dari beberapa karakter (teks) “Kotlinˮ Int 32 bits) Long 64 bits) Angka Integer 231 until 231 - 1 263 until 263 - 1 Float 32 bits) Double 64 bits) Angka Fraksional 7.7f 7.7 Boolean Hanya berisi 2 nilai saja true/false
  10. // val vs var val name: String = "Dico" name

    = "Codi" // Error: cannot be reassigned var age: Int = 7 age++ // value can be re-assigned to 8 // type inference val score = 100 println(score::class.simpleName) // Output: Int // built-in function println(name.uppercase()) // Output: DICO println(score.plus(6)) // Output: 106 // concatenate string vs string template println("Number of char in " + name + " is " + name.length) println("Number of char in $name is ${name.length}") Tipe Data
  11. Nullable Types Nullable types dalam Kotlin memungkinkan sebuah variabel untuk

    diatur ke nilai tipe data tersebut, jika ada, atau ke nilai khusus NULL, jika tidak ada. val grade: Int? = null // Run even if returned null 🤩 // Output: null val grade: Int = null // Error if returned null ❌ // Error: NullPointerException
  12. Quiz #1  DevCoach 187 Berapa nilai akhir dari variabel

    Score? a. null b. 10 c. 11 d. Program error Kotlin Format jawaban: #quiz1-username-jawaban
  13. If Statement Jika kondisinya true, maka akan mengeksekusi apa yang

    ada di dalam code block. Sedangkan, jika dalam kondisi false, akan masuk ke langkah berikutnya. val openHours = 7 val now = 7 if (now > openHours) { print("Office already open") } else if (now == openHours){ print("Wait a minute, office will be open") } else { print("Office is closed") } // Output : "Wait a minute, office will be open" // because first condition false and second condition is true
  14. If Statement and If Expression Comparison val openHours = 7

    val now = 7 val office: String if (now > openHours) { print("Office already open") } else if (now == openHours){ print("Wait a minute") } else { print("Office is closed") } If Statement If Expression val openHours = 7 val now = 7 val office: String office = if(now > openHours) { "Office already open" } else if (now == openHours){ "Wait a minute" } else { "Office is closed" } print(office)
  15. When Expression Karena sebuah expression, when dapat mengembalikkan nilai dan

    nilai tersebut dapat ditampung ke dalam variable. Contohnya seperti berikut: fun main() { val value = 7 val stringOfValue = when (value) { 6 -> "value is 6" 7 -> "value is 7" 8 -> "value is 8" else -> "value cannot be reached" } println(stringOfValue) } //output : value is 7
  16. Contoh Lain dari When Statement val anyType : Any =

    100L when(anyType){ is Long -> println("the value has a Long type") is String -> println("the value has a String type") } • Memastikan tipe data dari sebuah nilai val value = 27 val ranges = 10..50 when(value){ in ranges -> println("value is in the range") !in ranges -> println("value is outside the range") } • Memeriksa dalam jangkauan tertentu
  17. Loops fun main() { var counter = 1 while (counter

    <= 5){ println("Hello World") counter++ } } Mengulang kode berdasarkan kondisi tertentu = fun main() { println("Hello World") println("Hello World") println("Hello World") println("Hello World") println("Hello World") }
  18. Loops Mengulang kode berdasarkan kondisi tertentu var x = 5

    while (x < 5){ println("Hello World") x++ } For // iterate from range for (i in 0..5){ println(i) // iterate from array val array = arrayOf("A", "B", "C") for (element in array) { println(element) } array.forEach { element -> println(element) } var y = 5 do { println("Hello Kotlin") y++ } while (y < 5) Do..While While
  19. Function Sekumpulan kode yang dapat digabungkan untuk tujuan tertentu. fun

    main() { println("Hello Kotlin!") println(“Hello Dicoding!ˮ) }
  20. Sintaks Function fun setUser(name: String, age: Int): String { return

    "Your name is $name, and you $age years old" } Return Type (default is Unit) Function body (inside curly braces) Parameters (separated by comma) Function Name (lowerCamelCase) fun main() { val user = setUser("Arif", 17) println(user) }
  21. Function Sekumpulan kode yang dapat digabungkan untuk tujuan tertentu. -

    Function mengembalikan nilai - Function tidak mengembalikan nilai fun main() { println("Hello Kotlin!") println(“Hello Dicoding!ˮ) }
  22. Function Mengembalikan nilai fun setUser(name: String, age: Int): String {

    return "Your name is $name, and you $age years old" } Return Type (default is Unit) fun main() { val user = setUser("Arif", 17) println(user) // Output: Your name is Arif, and your age is 17 years old }
  23. Function Tidak Mengembalikan Nilai fun setUser(name: String, age: Int): Unit{

    println("Your name is $name, and your age is $age years old") } Return Type (default is Unit) fun main() { setUser("Arif", 17) // Output: Your name is Arif, and your age is 17 years old }
  24. Quiz #2 DevCoach 187 Apa istilah untuk data yang dikirimkan

    ke dalam fungsi? Kotlin a). Parameter b). Return Format jawaban: #quiz2-username-jawaban
  25. Quiz #3  DevCoach 187 Apa IDE yang digunakan untuk

    membuat program dengan bahasa Kotlin? Kotlin a). Open JDK IDE b). Gradle c). IntelliJ IDEA Format jawaban: #quiz3-username-jawaban