SlideShare ist ein Scribd-Unternehmen logo
1 von 38
Memulai
Pemrograman
dengan Kotlin
#2
Ahmad Arif Faizin
Academy Content Writer
Dicoding Indonesia
x
Tips Trik Seputar Instalasi
- Pastikan menggunakan versi yang sama dengan yang ada di modul.
- Jika sudah pernah menginstall Java sebelumnya, install dulu versi yang lama.
- Invalidate cache/restart jika tidak bisa run EduTools.
- Disable antivirus seperti Avast.
- Jika errornya “Failed to launch checking”, coba hapus folder project dan ulangi
mengambil course.
- Pastikan koneksi lancar saat instalasi.
- Kode merah-merah? pastikan besar kecil spasi sama dengan di modul.
- Untuk pengguna 32 bit bisa mencoba install JDK lewat Azul.com
3.
Kotlin Fundamental
Tipe Data & Fungsi
Hello Kotlin!
// main function
fun main() {
println("Hello Kotlin!")
}
fungsi pertama ketika
program dijalankan
untuk memberi komentar
fungsi untuk mencetak teks
ke layar/console
println vs print
fun main() {
println("Kotlin, ")
print("is Awesome!")
}
/*
output:
Kotlin,
is Awesome
*/
fun main() {
print("Kotlin, ")
print("is Awesome!")
}
/*
output:
Kotlin, is Awesome
*/
Contoh lain
fun main() {
val name = "Arif"
print("Hello my name is ")
println(name)
print(if (true) "Always true" else "Always false")
}
/*
output:
Hello my name is Arif
Always true
*/
cetak teks ke layar
untuk membuat variabel dengan tipe data string
untuk membuat
percabangan
cetak teks ke layar dan
membuat baris baru
Cara Menulis Variabel
var namaVariabel: TipeData = isi(value)
Contoh :
var company: String = "Dicoding"
atau bisa disingkat
var company = "Dicoding"
tipe data akan
menyesuaikan dengan
isinya
var vs val
Bisa diubah nilainya
(mutable)
var company = "Dicoding"
company = "Dicoding Academy"
Tidak bisa diubah nilainya
(immutable)
val company = "Dicoding"
company = "Dicoding Academy"
//Val cannot be reassigned
Tipe Data
Char hanya satu karakter ‘A’
String kumpulan dari banyak karakter (teks) “Kotlin”
Array menyimpan banyak objek arrayOf(“Aku”, “Kamu”, “Dia”)
Numbers angka 7
Boolean hanya memiliki 2 nilai true/false
Char
Contoh :
val character: Char = 'A'
Contoh salah
val character: Char = 'ABC'
// Incorrect character literal
Char
Operator increment & decrement
fun main() {
var vocal = 'A'
println("Vocal " + vocal++)
println("Vocal " + vocal--)
println("Vocal " + vocal)
}
/*
output:
Vocal A
Vocal B
Vocal A
*/
String
Memasukkan variabel ke dalam String
Contoh :
fun main() {
val text = "Kotlin"
val firstChar = text[0]
print("First character of" + text + " is " + firstChar)
}
print("First character of $text is $firstChar") string template
String
Membuat pengecualian(escaped) di dalam String
Contoh :
val statement = "Kotlin is "Awesome!""
String
Membuat Raw String
Contoh :
fun main() {
val line = """
Line 1
Line 2
Line 3
Line 4
""".trimIndent()
print(line)
}
/*
output:
Line 1
Line 2
Line 3
Line 4
*/
Array
indeks dimulai dari 0
fun main() {
val intArray = intArrayOf(1, 3, 5, 7) // [1, 3, 5, 7]
intArray[2] = 11 // [1, 3, 11, 7]
print(intArray[2])
}
/*
Output: 11
*/
1 3 5 7
ke-0 ke-1 ke-2 ke-3
Boolean
Numbers
Int (32 bit) nilai angka yang umum digunakan val intNumbers = 100
Long (64 bit) nilai angka yang besar val longNumbers = 100L
Short (16 bit) nilai angka yang kecil val shortNumber: Short = 10
Byte (8 bit) nilai angka yang sangat kecil val byteNumber = 0b11010010
Double (64 bit) nilai angka pecahan val doubleNumbers = 1.3
Functions
tanpa nilai kembalian
fun main() {
setUser("Arif", 17)
}
fun setUser(name: String, age: Int){
println("Your name is $name, and you $age years old")
}
parameter yang dikirimkan
ke dalam fungsi
untuk membuat fungsi
Functions
dengan nilai kembalian
fun main() {
val user = setUser("Arif", 17)
println(user)
}
fun setUser(name: String, age: Int): String {
return "Your name is $name, and you $age years old"
}
untuk membuat nilai
kembalian
tipe data nilai yang
dikembalikan
fun setUser(name: String, age: Int) = "Your name is $name, and you $age years old"
If Expressions
percabangan berdasarkan kondisi
val openHours = 7
val now = 20
if (now > openHours){
println("office already open")
}
val openHours = 7
val now = 20
val office: String
if (now > openHours) {
office = "Office already open"
} else {
office = "Office is closed"
}
print(office)
val openHours = 7
val now = 7
val office: String
office = if (now > 7) {
"Office already open"
} else if (now == openHours){
"Wait a minute, office will be open"
} else {
"Office is closed"
}
print(office)
Nullable Types
Contoh:
val text: String? = null
Contoh salah:
val text: String = null
// compile time error
Safe Call Operator ?.
memanggil nullable dengan aman
val text: String? = null
text?.length
val text: String? = null
val textLength = text?.length ?: 7
Elvis Operator ?:
nilai default jika objek bernilai null
Kerjakan Latihan
4.
Control Flow
Percabangan & Perulangan
When Expression
Percabangan selain If Else
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
*/
When Expression
val anyType : Any = 100L
when(anyType){
is Long -> println("the value has a Long type")
is String -> println("the value has a String type")
else -> println("undefined")
}
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")
else -> println("value undefined")
}
val x = 11
when (x) {
10, 11 -> print("the value between 10 and 11")
11, 12 -> print("the value between 11 and 12")
}
val x = 10
when (x) {
5 + 5 -> print("the value is 10")
10 + 10 -> print("the value is 20")
}
Enumeration
fun main() {
val color: Color = Color.GREEN
when(color){
Color.RED -> print("Color is Red")
Color.BLUE -> print("Color is Blue")
Color.GREEN -> print("Color is Green")
}
}
enum class Color(val value: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
//output : Color is Green
While
Perulangan
fun main() {
var counter = 1
while (counter <= 5){
println("Hello, World!")
counter++
}
}
fun main() {
println("Hello World")
println("Hello World")
println("Hello World")
println("Hello World")
println("Hello World")
}
=
fun main() {
var counter = 1
while (counter <= 7){
println("Hello, World!")
counter++
}
}
cek dulu
baru jalankan
While vs Do While
fun main() {
var counter = 1
do {
println("Hello, World!")
counter++
} while (counter <= 7)
}
jalankan dulu
baru cek
Hati - Hati dengan infinite loop
Karena kondisi tidak terpenuhi
var value = 'A'
do {
print(value)
} while (value <= 'Z')
var i = 3
while (i > 3) {
println(i)
i--
}
val rangeInt = 1.rangeTo(10)
=
val rangeInt = 1..10
=
val rangeInt = 1 until 10
dari bawah
ke atas
rangeTo vs downTo
val downInt = 10.downTo(1)
dari atas ke bawah
For Loop
Perulangan dengan counter otomatis
fun main() {
val ranges = 1..5
for (i in ranges){
println("value is $i!")
}
}
fun main() {
var counter = 1
while (counter <= 5){
println("value is $counter!")
counter++
}
}
=
For Each
Perulangan dengan counter otomatis
fun main() {
val ranges = 1..5
ranges.forEach{
println("value is $i!")
}
}
fun main() {
val ranges = 1..5
for (i in ranges){
println("value is $i!")
}
}
=
Break & Continue
Contoh program deteksi angka ganjil sampai angka 10
fun main() {
val listNumber = 1.rangeTo(50)
for (number in listNumber) {
if (number % 2 == 0) continue
if (number > 10) break
println("$number adalah bilangan ganjil")
}
}
* % adalah modulus / sisa pembagian
misal 10 % 2 == 0, 11 % 2 = 1 , 9 % 6 = 3
/*
output:
1 adalah bilangan ganjil
3 adalah bilangan ganjil
5 adalah bilangan ganjil
7 adalah bilangan ganjil
9 adalah bilangan ganjil
*/
Lanjutkan Latihan
&
Terus Belajar!
OUR LIFE SHOULD BE JUST LIKE
MODULUS. DOESN’T MATTER WHAT VALUE
IS ENTERED, IT’S RESULT ALWAYS
POSITIVE. AND SO SHOULD BE OUR LIFE
IN EVERY SITUATION
- Pranay Neve
’’
’’
You can find me at:
● Google : Ahmad Arif Faizin
● Discord : @arifaizin
Thanks!
Any questions?

Weitere ähnliche Inhalte

Was ist angesagt?

Program Pengurutan (Sorting) dan Pencarian (Searching) Data
Program Pengurutan  (Sorting) dan Pencarian  (Searching) DataProgram Pengurutan  (Sorting) dan Pencarian  (Searching) Data
Program Pengurutan (Sorting) dan Pencarian (Searching) Data
Simon Patabang
 

Was ist angesagt? (20)

Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Kotlin
KotlinKotlin
Kotlin
 
Kotlin - Better Java
Kotlin - Better JavaKotlin - Better Java
Kotlin - Better Java
 
Modul Praktikum Pemrograman Berorientasi Objek (Chap.1-6)
Modul Praktikum Pemrograman Berorientasi Objek (Chap.1-6)Modul Praktikum Pemrograman Berorientasi Objek (Chap.1-6)
Modul Praktikum Pemrograman Berorientasi Objek (Chap.1-6)
 
JDBC
JDBCJDBC
JDBC
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
 
Laporan modul 5 basisdata
Laporan modul 5 basisdataLaporan modul 5 basisdata
Laporan modul 5 basisdata
 
OOP V3.1
OOP V3.1OOP V3.1
OOP V3.1
 
Introduction to Coroutines @ KotlinConf 2017
Introduction to Coroutines @ KotlinConf 2017Introduction to Coroutines @ KotlinConf 2017
Introduction to Coroutines @ KotlinConf 2017
 
Flutter tutorial for Beginner Step by Step
Flutter tutorial for Beginner Step by StepFlutter tutorial for Beginner Step by Step
Flutter tutorial for Beginner Step by Step
 
Declarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeDeclarative UIs with Jetpack Compose
Declarative UIs with Jetpack Compose
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
Pertemuan 6 tabview
Pertemuan 6 tabviewPertemuan 6 tabview
Pertemuan 6 tabview
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Program Pengurutan (Sorting) dan Pencarian (Searching) Data
Program Pengurutan  (Sorting) dan Pencarian  (Searching) DataProgram Pengurutan  (Sorting) dan Pencarian  (Searching) Data
Program Pengurutan (Sorting) dan Pencarian (Searching) Data
 
Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency Injection
 
Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017Deep dive into Coroutines on JVM @ KotlinConf 2017
Deep dive into Coroutines on JVM @ KotlinConf 2017
 
Modul praktikum basis data new
Modul praktikum basis data newModul praktikum basis data new
Modul praktikum basis data new
 

Ähnlich wie Dts x dicoding #2 memulai pemrograman kotlin

C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
C++ course start
C++ course startC++ course start
C++ course start
Net3lem
 

Ähnlich wie Dts x dicoding #2 memulai pemrograman kotlin (20)

Introduction to Kotlin.pptx
Introduction to Kotlin.pptxIntroduction to Kotlin.pptx
Introduction to Kotlin.pptx
 
01 Introduction to Kotlin - Programming in Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx01 Introduction to Kotlin - Programming in Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
 
InterConnect: Server Side Swift for Java Developers
InterConnect:  Server Side Swift for Java DevelopersInterConnect:  Server Side Swift for Java Developers
InterConnect: Server Side Swift for Java Developers
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Swift Study #7
Swift Study #7Swift Study #7
Swift Study #7
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
Kotlin
KotlinKotlin
Kotlin
 
Introduction to Go for Java Programmers
Introduction to Go for Java ProgrammersIntroduction to Go for Java Programmers
Introduction to Go for Java Programmers
 
C++ course start
C++ course startC++ course start
C++ course start
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage Go
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181The Ring programming language version 1.5.2 book - Part 14 of 181
The Ring programming language version 1.5.2 book - Part 14 of 181
 
Something about Golang
Something about GolangSomething about Golang
Something about Golang
 
Ruby
RubyRuby
Ruby
 
Loops
LoopsLoops
Loops
 
Python
PythonPython
Python
 
The Ring programming language version 1.5.1 book - Part 22 of 180
The Ring programming language version 1.5.1 book - Part 22 of 180The Ring programming language version 1.5.1 book - Part 22 of 180
The Ring programming language version 1.5.1 book - Part 22 of 180
 

Mehr von Ahmad Arif Faizin

Mehr von Ahmad Arif Faizin (20)

Proker Departemen Dakwah dan Syiar 2013.pptx
Proker Departemen Dakwah dan Syiar 2013.pptxProker Departemen Dakwah dan Syiar 2013.pptx
Proker Departemen Dakwah dan Syiar 2013.pptx
 
DKM_2013_BISMILLAH.pptx
DKM_2013_BISMILLAH.pptxDKM_2013_BISMILLAH.pptx
DKM_2013_BISMILLAH.pptx
 
Proker bendahara al muhandis 2013.ppt
Proker bendahara al muhandis 2013.pptProker bendahara al muhandis 2013.ppt
Proker bendahara al muhandis 2013.ppt
 
PPT raker EKONOMI 2013.pptx
PPT raker EKONOMI 2013.pptxPPT raker EKONOMI 2013.pptx
PPT raker EKONOMI 2013.pptx
 
Program Kerja Kaderisasi Al Muhandis 2013
Program Kerja Kaderisasi Al Muhandis 2013Program Kerja Kaderisasi Al Muhandis 2013
Program Kerja Kaderisasi Al Muhandis 2013
 
Departemen Mentoring.pptx
Departemen Mentoring.pptxDepartemen Mentoring.pptx
Departemen Mentoring.pptx
 
ANNISAA' 2013.pptx
ANNISAA' 2013.pptxANNISAA' 2013.pptx
ANNISAA' 2013.pptx
 
PPT KKN PEDURUNGAN 2016.pptx
PPT KKN PEDURUNGAN 2016.pptxPPT KKN PEDURUNGAN 2016.pptx
PPT KKN PEDURUNGAN 2016.pptx
 
Absis UNBK.pptx
Absis UNBK.pptxAbsis UNBK.pptx
Absis UNBK.pptx
 
Dsc how google programs make great developer
Dsc how google programs make great developerDsc how google programs make great developer
Dsc how google programs make great developer
 
First Gathering Sandec
First Gathering SandecFirst Gathering Sandec
First Gathering Sandec
 
Mockup Android Application Template Library
Mockup Android Application Template LibraryMockup Android Application Template Library
Mockup Android Application Template Library
 
Mockup Android Application : Go bon
Mockup Android Application : Go bonMockup Android Application : Go bon
Mockup Android Application : Go bon
 
Lomba Sayembara Logo
Lomba Sayembara LogoLomba Sayembara Logo
Lomba Sayembara Logo
 
Template Video Invitation Walimatul Ursy
Template Video Invitation Walimatul UrsyTemplate Video Invitation Walimatul Ursy
Template Video Invitation Walimatul Ursy
 
Training Android Wonderkoding
Training Android WonderkodingTraining Android Wonderkoding
Training Android Wonderkoding
 
The Best Way to Become an Android Developer Expert with Android Jetpack
The Best Way to Become an Android Developer Expert  with Android JetpackThe Best Way to Become an Android Developer Expert  with Android Jetpack
The Best Way to Become an Android Developer Expert with Android Jetpack
 
Mengapa Perlu Belajar Coding?
Mengapa Perlu Belajar Coding?Mengapa Perlu Belajar Coding?
Mengapa Perlu Belajar Coding?
 
PPT Seminar TA Augmented Reality
PPT Seminar TA Augmented RealityPPT Seminar TA Augmented Reality
PPT Seminar TA Augmented Reality
 
Android intermediatte Full
Android intermediatte FullAndroid intermediatte Full
Android intermediatte Full
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 

Dts x dicoding #2 memulai pemrograman kotlin

  • 1. Memulai Pemrograman dengan Kotlin #2 Ahmad Arif Faizin Academy Content Writer Dicoding Indonesia x
  • 2. Tips Trik Seputar Instalasi - Pastikan menggunakan versi yang sama dengan yang ada di modul. - Jika sudah pernah menginstall Java sebelumnya, install dulu versi yang lama. - Invalidate cache/restart jika tidak bisa run EduTools. - Disable antivirus seperti Avast. - Jika errornya “Failed to launch checking”, coba hapus folder project dan ulangi mengambil course. - Pastikan koneksi lancar saat instalasi. - Kode merah-merah? pastikan besar kecil spasi sama dengan di modul. - Untuk pengguna 32 bit bisa mencoba install JDK lewat Azul.com
  • 3.
  • 5. Hello Kotlin! // main function fun main() { println("Hello Kotlin!") } fungsi pertama ketika program dijalankan untuk memberi komentar fungsi untuk mencetak teks ke layar/console
  • 6. println vs print fun main() { println("Kotlin, ") print("is Awesome!") } /* output: Kotlin, is Awesome */ fun main() { print("Kotlin, ") print("is Awesome!") } /* output: Kotlin, is Awesome */
  • 7. Contoh lain fun main() { val name = "Arif" print("Hello my name is ") println(name) print(if (true) "Always true" else "Always false") } /* output: Hello my name is Arif Always true */ cetak teks ke layar untuk membuat variabel dengan tipe data string untuk membuat percabangan cetak teks ke layar dan membuat baris baru
  • 8. Cara Menulis Variabel var namaVariabel: TipeData = isi(value) Contoh : var company: String = "Dicoding" atau bisa disingkat var company = "Dicoding" tipe data akan menyesuaikan dengan isinya
  • 9. var vs val Bisa diubah nilainya (mutable) var company = "Dicoding" company = "Dicoding Academy" Tidak bisa diubah nilainya (immutable) val company = "Dicoding" company = "Dicoding Academy" //Val cannot be reassigned
  • 10. Tipe Data Char hanya satu karakter ‘A’ String kumpulan dari banyak karakter (teks) “Kotlin” Array menyimpan banyak objek arrayOf(“Aku”, “Kamu”, “Dia”) Numbers angka 7 Boolean hanya memiliki 2 nilai true/false
  • 11. Char Contoh : val character: Char = 'A' Contoh salah val character: Char = 'ABC' // Incorrect character literal
  • 12. Char Operator increment & decrement fun main() { var vocal = 'A' println("Vocal " + vocal++) println("Vocal " + vocal--) println("Vocal " + vocal) } /* output: Vocal A Vocal B Vocal A */
  • 13. String Memasukkan variabel ke dalam String Contoh : fun main() { val text = "Kotlin" val firstChar = text[0] print("First character of" + text + " is " + firstChar) } print("First character of $text is $firstChar") string template
  • 14. String Membuat pengecualian(escaped) di dalam String Contoh : val statement = "Kotlin is "Awesome!""
  • 15. String Membuat Raw String Contoh : fun main() { val line = """ Line 1 Line 2 Line 3 Line 4 """.trimIndent() print(line) } /* output: Line 1 Line 2 Line 3 Line 4 */
  • 16. Array indeks dimulai dari 0 fun main() { val intArray = intArrayOf(1, 3, 5, 7) // [1, 3, 5, 7] intArray[2] = 11 // [1, 3, 11, 7] print(intArray[2]) } /* Output: 11 */ 1 3 5 7 ke-0 ke-1 ke-2 ke-3
  • 18. Numbers Int (32 bit) nilai angka yang umum digunakan val intNumbers = 100 Long (64 bit) nilai angka yang besar val longNumbers = 100L Short (16 bit) nilai angka yang kecil val shortNumber: Short = 10 Byte (8 bit) nilai angka yang sangat kecil val byteNumber = 0b11010010 Double (64 bit) nilai angka pecahan val doubleNumbers = 1.3
  • 19. Functions tanpa nilai kembalian fun main() { setUser("Arif", 17) } fun setUser(name: String, age: Int){ println("Your name is $name, and you $age years old") } parameter yang dikirimkan ke dalam fungsi untuk membuat fungsi
  • 20. Functions dengan nilai kembalian fun main() { val user = setUser("Arif", 17) println(user) } fun setUser(name: String, age: Int): String { return "Your name is $name, and you $age years old" } untuk membuat nilai kembalian tipe data nilai yang dikembalikan fun setUser(name: String, age: Int) = "Your name is $name, and you $age years old"
  • 21. If Expressions percabangan berdasarkan kondisi val openHours = 7 val now = 20 if (now > openHours){ println("office already open") } val openHours = 7 val now = 20 val office: String if (now > openHours) { office = "Office already open" } else { office = "Office is closed" } print(office) val openHours = 7 val now = 7 val office: String office = if (now > 7) { "Office already open" } else if (now == openHours){ "Wait a minute, office will be open" } else { "Office is closed" } print(office)
  • 22. Nullable Types Contoh: val text: String? = null Contoh salah: val text: String = null // compile time error
  • 23. Safe Call Operator ?. memanggil nullable dengan aman val text: String? = null text?.length val text: String? = null val textLength = text?.length ?: 7 Elvis Operator ?: nilai default jika objek bernilai null
  • 26. When Expression Percabangan selain If Else 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 */
  • 27. When Expression val anyType : Any = 100L when(anyType){ is Long -> println("the value has a Long type") is String -> println("the value has a String type") else -> println("undefined") } 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") else -> println("value undefined") } val x = 11 when (x) { 10, 11 -> print("the value between 10 and 11") 11, 12 -> print("the value between 11 and 12") } val x = 10 when (x) { 5 + 5 -> print("the value is 10") 10 + 10 -> print("the value is 20") }
  • 28. Enumeration fun main() { val color: Color = Color.GREEN when(color){ Color.RED -> print("Color is Red") Color.BLUE -> print("Color is Blue") Color.GREEN -> print("Color is Green") } } enum class Color(val value: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } //output : Color is Green
  • 29. While Perulangan fun main() { var counter = 1 while (counter <= 5){ println("Hello, World!") counter++ } } fun main() { println("Hello World") println("Hello World") println("Hello World") println("Hello World") println("Hello World") } =
  • 30. fun main() { var counter = 1 while (counter <= 7){ println("Hello, World!") counter++ } } cek dulu baru jalankan While vs Do While fun main() { var counter = 1 do { println("Hello, World!") counter++ } while (counter <= 7) } jalankan dulu baru cek
  • 31. Hati - Hati dengan infinite loop Karena kondisi tidak terpenuhi var value = 'A' do { print(value) } while (value <= 'Z') var i = 3 while (i > 3) { println(i) i-- }
  • 32. val rangeInt = 1.rangeTo(10) = val rangeInt = 1..10 = val rangeInt = 1 until 10 dari bawah ke atas rangeTo vs downTo val downInt = 10.downTo(1) dari atas ke bawah
  • 33. For Loop Perulangan dengan counter otomatis fun main() { val ranges = 1..5 for (i in ranges){ println("value is $i!") } } fun main() { var counter = 1 while (counter <= 5){ println("value is $counter!") counter++ } } =
  • 34. For Each Perulangan dengan counter otomatis fun main() { val ranges = 1..5 ranges.forEach{ println("value is $i!") } } fun main() { val ranges = 1..5 for (i in ranges){ println("value is $i!") } } =
  • 35. Break & Continue Contoh program deteksi angka ganjil sampai angka 10 fun main() { val listNumber = 1.rangeTo(50) for (number in listNumber) { if (number % 2 == 0) continue if (number > 10) break println("$number adalah bilangan ganjil") } } * % adalah modulus / sisa pembagian misal 10 % 2 == 0, 11 % 2 = 1 , 9 % 6 = 3 /* output: 1 adalah bilangan ganjil 3 adalah bilangan ganjil 5 adalah bilangan ganjil 7 adalah bilangan ganjil 9 adalah bilangan ganjil */
  • 37. OUR LIFE SHOULD BE JUST LIKE MODULUS. DOESN’T MATTER WHAT VALUE IS ENTERED, IT’S RESULT ALWAYS POSITIVE. AND SO SHOULD BE OUR LIFE IN EVERY SITUATION - Pranay Neve ’’ ’’
  • 38. You can find me at: ● Google : Ahmad Arif Faizin ● Discord : @arifaizin Thanks! Any questions?