Thuta Learning
Kotlin
BasicProgrammingအခြေခံ

What is Kotlin?

Kotlinသင်ခန်းစာ 220 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Language (Kotlin) နဲ့ platform (JVM, Android, JS, Native) ကို ခွဲပြောနိုင်ဖို့
  • Kotlin ရဲ့ ရည်မှန်းချက်လေးခု (concise, safe, interoperable, tool-friendly) ကို code ဥပမာနဲ့ ပြနိုင်ဖို့
  • `String` နဲ့ `String?` ကွာခြားချက်ကို ရှင်းပြပြီး compile error ဖြစ်မဖြစ် ကြိုမှန်းနိုင်ဖို့
  • Java နဲ့ Kotlin တူတဲ့နေရာ၊ ကွဲတဲ့နေရာကို အတိုချုပ် ပြောနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

What is Kotlin ကို ရိုးရိုးလေး ပြောရရင်

Java နဲ့ Kotlin ကို အဆောက်အအုံတစ်ခုနဲ့ ယှဉ်ကြည့်ပါ။ JVM က ခိုင်ခံ့ပြီး လူတွေ နှစ်ပေါင်းများစွာ သုံးလာခဲ့တဲ့ အဆောက်အအုံပါ။ Java က အဲဒီအဆောက်အအုံထဲ ဝင်တဲ့ တံခါးဟောင်းဖြစ်ပြီး Kotlin က ပိုခေတ်မီပြီး ပိုလုံခြုံတဲ့ တံခါးသစ်ပါ။ တံခါးနှစ်ခုလုံးက အတွင်းအခန်းတွေ၊ ပရိဘောဂတွေ (library တွေ) ကို အတူတူ မျှသုံးနိုင်ပါတယ်။

Kotlin ကို ရေးတဲ့သူရဲ့ ရှုထောင့်ကနေ ကြည့်ရင် အဓိက ကောင်းကွက်နှစ်ခုက code တိုတာနဲ့ ရှိမနေတဲ့ value ကြောင့် ဖြစ်တဲ့ crash တွေကို compile လုပ်တဲ့အချိန်မှာတင် ဖမ်းပေးတာပါ။ အဲဒီနှစ်ခုကို ဒီ lesson မှာ ဥပမာနဲ့ တွေ့ရပါမယ်။

Problem → Why → Solution

ပြဿနာ: boilerplate အများကြီးနဲ့ run time ကျမှ ပေါ်တဲ့ crash

Java မှာ ခေါင်းစဉ်နဲ့ စာမျက်နှာအရေအတွက်ပဲ သိမ်းတဲ့ class သေးသေးလေးတစ်ခု ရေးရင်တောင် field၊ constructor၊ getter တွေ ရေးရပါတယ်၊ ပြီးတော့ `equals()`, `hashCode()`, `toString()` တွေ ထပ်လိုပါတယ်။ ရေးထားတဲ့ line တွေထဲမှာ ကိုယ်တကယ်ပြောချင်တာ ('Book တစ်အုပ်မှာ title နဲ့ pages ရှိတယ်') က ကျန်တာတွေအောက်မှာ ပျောက်သွားပါတယ်။

ဒုတိယပြဿနာက ပိုဆိုးပါတယ်။ အောက်က code ရဲ့ နောက်ဆုံး line ဟာ Java မှာ compile ဖြစ်ပါတယ်၊ ဒါပေမယ့် `subtitle` က `null` ဖြစ်နေတာမို့ program run တဲ့အခါမှ `NullPointerException` နဲ့ crash ဖြစ်ပါတယ်။ User ဆီ ရောက်မှ ပေါ်လာတဲ့ အမှားမျိုးပါ။

Book.java
public class Book {
    private final String title;
    private final int pages;

    public Book(String title, int pages) {
        this.title = title;
        this.pages = pages;
    }

    public String getTitle() { return title; }
    public int getPages() { return pages; }
    // ... plus equals(), hashCode() and toString()
}

// somewhere else:
String subtitle = null;
int length = subtitle.length();   // compiles, then NullPointerException

Kotlin က ဒီနှစ်ခုကို ဘယ်လို ဖြေရှင်းလဲ

Boilerplate အတွက် Kotlin မှာ `data class` ရှိပြီး field တွေကို ကြေညာလိုက်ရုံနဲ့ getter, `equals()`, `hashCode()`, `toString()` တွေကို compiler က ထုတ်ပေးပါတယ်။ Null အတွက်တော့ type system ကိုယ်တိုင်က `String` (null မဖြစ်နိုင်) နဲ့ `String?` (null ဖြစ်နိုင်) ကို ခွဲထားလို့ ပျောက်နေနိုင်တဲ့ value ကို မစစ်ဘဲ သုံးရင် compile လုပ်တဲ့အချိန်မှာတင် error ပေးပါတယ်။

အဓိက သဘောတရား: ခိုင်မာတဲ့ platform ပေါ်က ခေတ်သစ် language

အရင်ဆုံး language နဲ့ platform ကို ခွဲမြင်ပါ။ Kotlin ဆိုတာ syntax၊ compiler (`kotlinc`) နဲ့ standard library ပေါင်းထားတာပါ။ JVM ကတော့ bytecode ကို run ပေးတဲ့ platform ပါ။ Kotlin က JVM ကို အစားမထိုးပါဘူး၊ JVM ထဲ ဝင်ဖို့ တံခါးသစ်တစ်ခု ဖွင့်ပေးတာပါ။ ဒါကြောင့် Java နဲ့ Kotlin ဟာ bytecode တူကို ထုတ်ပေးပြီး library အကုန်ကို မျှသုံးနိုင်ပါတယ်။

Kotlin ကို ဒီဇိုင်းလုပ်တဲ့အခါ ရည်မှန်းချက်လေးခု ရှိပါတယ်။ အောက်ပါ table မှာ တစ်ခုချင်းစီကို code မှာ ဘယ်လိုမြင်ရလဲ ပြထားပါတယ်။

ရည်မှန်းချက်Code မှာ ဘယ်လိုမြင်ရလဲ
Concise`data class Book(val title: String, val pages: Int)` တစ်ကြောင်းတည်းက အပေါ်က Java class တစ်ခုလုံးကို အစားထိုးတယ်
Safe`String` ထဲမှာ null ထည့်လို့မရဘူး၊ `String?` ကို သုံးမယ်ဆိုရင် compiler က စစ်ခိုင်းတယ်
InteroperableJava library ကို Kotlin ကနေ တိုက်ရိုက် ခေါ်လို့ရပြီး Java ကလည်း Kotlin ကို ခေါ်လို့ရတယ်
Tool-friendlyIDE ကုမ္ပဏီက ဖန်တီးထားလို့ completion, refactoring, Java-to-Kotlin conversion တွေ ကောင်းတယ်

Compiler က ရှိမနေနိုင်တဲ့ value ကို ဘယ်လို ကာကွယ်ပေးလဲ ကြည့်ရအောင်။ `val subtitle: String = null` လို့ ရေးရင် `null cannot be a value of a non-null type 'String'` လို့ compile error ပေါ်ပါတယ်။ `String?` လို့ ကြေညာပြီးတော့ `subtitle.length` လို တိုက်ရိုက် ခေါ်ရင်လည်း `only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type 'String?'` လို့ ပြောပါတယ်။ Java မှာ run time မှ ကြုံရမယ့် crash ကို Kotlin မှာ ရေးနေတုန်းမှာတင် တွေ့ရတာပါ။

Target တွေကိုလည်း သိထားသင့်ပါတယ်။ Kotlin/JVM က bytecode ထုတ်ပြီး server, desktop နဲ့ tool တွေအတွက် သုံးပါတယ်။ Android app တွေကိုလည်း Kotlin နဲ့ ရေးလို့ရပါတယ်။ Kotlin/JS က JavaScript ထုတ်ပြီး Kotlin/Native က JVM မလိုတဲ့ native binary ထုတ်ပါတယ်။ Kotlin Multiplatform ကတော့ shared module တစ်ခုကို အဲဒီ target တွေအတွက် တစ်ခုချင်း compile လုပ်ပေးပါတယ်။ ဒီ course ရဲ့ example အားလုံးက Kotlin/JVM ပေါ်မှာ run ပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: တံခါးသစ်၊ အဆောက်အအုံဟောင်း
  1. Kotlin နဲ့ ရေးတယ်
  2. Java နဲ့ bytecode တူကို ထုတ်တယ်
  3. ရှိပြီးသား Java library တွေ သုံးလို့ရတယ်
  4. Java ကလည်း ပြန်ခေါ်နိုင်တယ်

Kotlin ကို သင်ယူတာက ဘာသာစကားသစ်ကို သင်တာဖြစ်ပြီး ဒါပေမယ့် ecosystem တစ်ခုလုံးကို ပြန်စရတာ မဟုတ်ပါဘူး။

အထွေထွေ ပုံစံ: top-level declaration တွေ

kotlin
data class TypeName(val field: FieldType)

fun helperName(param: ParamType): ResultType {
    return expression
}

fun main() {
    println(helperName(value))
}

Kotlin file တစ်ခုရဲ့ top level မှာ class၊ function တွေ တိုက်ရိုက်ရေးလို့ရပြီး အားလုံးကို ထုပ်ပိုးမယ့် class ကြီးတစ်ခု မလိုပါဘူး။ Program က `main` function ကနေ စပါတယ်။

Kotlin ကို မြည်းစမ်းကြည့်ခြင်း

ဒီ program က ဒီ lesson ရဲ့ အယူအဆတွေကို တစ်နေရာတည်း စုပြထားပါတယ်: data class၊ list၊ lambda နဲ့ filter၊ for loop၊ nullable type နဲ့ safe call။ Symbol တိုင်းကို အခုပဲ နားလည်ရမယ်လို့ မထင်ပါနဲ့ — တစ်ခုချင်းစီက နောက် lesson တွေမှာ သီးသန့် ပါလာပါမယ်။ ကြည့်ရမယ့်အချက်က code ဘယ်လောက် တိုပြီး ဘယ်လောက် ဖတ်လို့ရလဲ ဆိုတာပါ။

Library.kt
data class Book(val title: String, val pages: Int)

fun main() {
    val shelf = listOf(
        Book("Kotlin in a Day", 120),
        Book("Null Safety", 80),
        Book("Coroutines", 210)
    )

    val long = shelf.filter { it.pages > 100 }
    for (book in long) {
        println("${book.title} has ${book.pages} pages")
    }

    val subtitle: String? = null
    println("Subtitle length: ${subtitle?.length ?: 0}")
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. A data classdata class Book(val title: String, val pages: Int)

    ဒီ line တစ်ကြောင်းတည်းက Java မှာ ရေးရတဲ့ field၊ constructor၊ getter၊ `equals()`၊ `hashCode()`၊ `toString()` အားလုံးကို compiler ကို ထုတ်ခိုင်းလိုက်တာပါ။ `Book("Null Safety", 80)` လို့ ရေးရုံနဲ့ object တစ်ခု ရပါတယ်။

  2. A list of objectsval shelf = listOf(

    `listOf(...)` က ပြောင်းလို့မရတဲ့ list တစ်ခု ဖန်တီးပါတယ်။ `shelf` ရဲ့ type ကို `List<Book>` လို့ Kotlin က ကိုယ်တိုင် သိတာမို့ ရေးပေးစရာ မလိုပါဘူး။

  3. A lambda in filterval long = shelf.filter { it.pages > 100 }

    ကွင်းကောက်ထဲက `it.pages > 100` က lambda ပါ — item တစ်ခုချင်းအတွက် ကျင့်သုံးမယ့် အလုပ်သေးသေးလေးပါ။ `it` က လက်ရှိ Book ဖြစ်ပြီး စစ်ချက်မှန်တဲ့ (pages 100 ထက်များတဲ့) အုပ်တွေကိုပဲ ထားပါတယ်။

  4. A for loopfor (book in long) {

    `for (book in long)` က စာရင်းထဲက အုပ်တစ်အုပ်ချင်းကို `book` ဆိုတဲ့ နာမည်နဲ့ ပေးပြီး loop body ကို အကြိမ်ကြိမ် run ပါတယ်။ ဒီမှာ 120 နဲ့ 210 pages ပါတဲ့ အုပ်နှစ်အုပ်ကို print ထုတ်ပါတယ်။

  5. A nullable typeval subtitle: String? = null

    `String?` ထဲက `?` က ဒီ variable ထဲမှာ `null` ရှိနိုင်တယ်လို့ ကြေညာတာပါ။ `String` နဲ့ `String?` ဟာ type နှစ်မျိုး ကွဲပြီး compiler က ခွဲစစ်ပါတယ်။

  6. Safe call and defaultprintln("Subtitle length: ${subtitle?.length ?: 0}")

    `subtitle?.length` က subtitle က null မဟုတ်မှ length ကို ယူပြီး null ဆိုရင် crash မဖြစ်ဘဲ null ပြန်ပါတယ်။ `?: 0` ကတော့ ရလဒ်က null ဖြစ်ရင် 0 ကို အစားသုံးပါတယ်။ ဒါကြောင့် output က `0` ပါ။

You should see
Kotlin in a Day has 120 pages
Coroutines has 210 pages
Subtitle length: 0

Java နဲ့ Kotlin တူတဲ့ idea တွေကို ယှဉ်ကြည့်ခြင်း

Java ကို သိပြီးသား လူတွေအတွက်ရော၊ Java ကို မသိသေးတဲ့ လူတွေအတွက်ပါ ဒီ table က အသုံးဝင်ပါတယ်။ Kotlin ကို Java နဲ့ ကျယ်ကျယ်ပြန့်ပြန့် တွဲသုံးကြတာမို့ library docs တွေမှာ Java code တွေ့ရင် Kotlin ဘယ်လို ဖြစ်မလဲ ဘာသာပြန်ဖတ်နိုင်ဖို့ပါ။

IdeaJavaKotlin
မပြောင်းတဲ့ နာမည်final String name = "Aye";val name = "Aye"
Line တစ်ကြောင်း printSystem.out.println(name);println(name)
စာသားထဲ value ထည့်"Hi " + name + "!""Hi $name!"
Object ဖန်တီးnew Book("Kotlin", 120)Book("Kotlin", 120)

ပုံစံတူတာတွေ အများကြီးပါ — `if`, `for`, `class` တို့လို အခြေခံ အယူအဆတွေ အတူတူပါပဲ။ ကွာတာက Kotlin မှာ `new` မလိုတာ၊ semicolon မလိုတာ၊ `main` ကို class ထဲမှာ ထည့်စရာ မလိုတာတွေပါ။ Runtime ကတော့ တူပါတယ်၊ garbage collector နဲ့ JDK standard library ကို နှစ်ခုလုံး မျှသုံးပါတယ်။

လက်တွေ့ project တွေမှာ

လက်တွေ့မှာ Java နဲ့ Kotlin ကို ဘယ်လို ပေါင်းသုံးလဲ

Java နဲ့ Kotlin က bytecode တူတာကြောင့် team တွေက Java project ကြီးတစ်ခုလုံးကို ပြန်ရေးကြတာ မဟုတ်ပါဘူး။ ရှိပြီးသား `.java` file တွေနားမှာ `.kt` file အသစ်တွေ ထည့်သွားပြီး project တစ်ခုတည်းထဲ နှစ်မျိုးလုံး ပူးပေါင်း compile လုပ်ကြပါတယ်။ IntelliJ IDEA မှာ Java file တစ်ခုကို Kotlin file အဖြစ် ပြောင်းပေးတဲ့ feature လည်း ပါပါတယ်။ ဒါပေမယ့် ပြောင်းပေးထားတာက အစပြုရာသာ ဖြစ်တာမို့ Kotlin ပုံစံကောင်းကောင်း ပြန်ပြင်ဖို့ လိုတတ်ပါတယ်။

Library docs အများစုက Java နဲ့ ရေးထားတာမို့ Kotlin developer တိုင်းက Java code ကို ဖတ်နိုင်ဖို့ လိုပါတယ်။ ဒီလို ဖတ်တတ်ရင် Spring၊ Android SDK နဲ့ အခြား JVM library တွေကို Kotlin ကနေ မတွန့်ဆုတ်ဘဲ သုံးနိုင်ပါတယ်။ Code review မှာလည်း boilerplate နည်းပြီး `String` နဲ့ `String?` ခွဲထားလို့ reviewer က null ကို စစ်ထားလားဆိုတာ type ကိုကြည့်ရုံနဲ့ မြင်ရပါတယ်။ ဒီအားသာချက်တွေက ကြီးမားတဲ့ codebase တွေမှာ ပိုထင်ရှားပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Kotlin ကို Java ပုံစံနဲ့ ရေးတာ

Getter/setter method တွေ ကိုယ်တိုင်ရေးတာ၊ `new` ထည့်ရေးတာ၊ semicolon တွေ ဆက်ထည့်တာမျိုးက Kotlin မှာ အလုပ်မလုပ်တာ မဟုတ်ပေမယ့် Kotlin ရဲ့ အားသာချက်တွေကို သုံးမနေတာပါ။ Property၊ data class နဲ့ default value တွေကို သုံးပြီး ရိုးရှင်းအောင် ရေးပါ။

2. Null safety က NullPointerException ကို လုံးဝ ပပျောက်စေတယ်လို့ ထင်တာ

Kotlin code အတွင်းမှာတော့ compiler က စောင့်ကြည့်ပေးပါတယ်။ ဒါပေမယ့် Java library က ပြန်ပေးတဲ့ value တွေကို Kotlin က null ဖြစ်မဖြစ် သေချာ မသိနိုင်ပါဘူး (platform type လို့ ခေါ်ပါတယ်)၊ `!!` ကိုလည်း သုံးရင် compiler ကို ကိုယ်တိုင် မစစ်ခိုင်းလိုက်တာပါ။ အဲဒီနေရာတွေမှာ crash ပြန်ဖြစ်နိုင်ပါတယ်။

3. Kotlin ကို တခြား runtime လိုတယ်လို့ ထင်တာ

Kotlin/JVM program က ပုံမှန် JVM ပေါ်မှာ run ပါတယ်။ လိုတာက JDK (သို့ JRE) နဲ့ Kotlin standard library ပါ။ Standard library ကို jar ထဲ ထည့်ပေးလိုက်တာ (`-include-runtime`) က Get Started lesson မှာ ပါလာပါမယ်။

Mini challenge

Mini challenge: ကိုယ်ပိုင် playlist

Song data class တစ်ခု ဆောက်ပြီး ၃ မိနစ်ထက် ရှည်တဲ့ သီချင်းတွေကိုပဲ စစ်ထုတ် print ထုတ်ပါ၊ ပြီးရင် null ဖြစ်နိုင်တဲ့ guest artist တစ်ခုကို ဘေးကင်းစွာ print ထုတ်ပါ။

  • `title` (String) နဲ့ `seconds` (Int) ပါတဲ့ `Song` data class တစ်ခု ကြေညာပါ
  • `listOf` နဲ့ သီချင်းသုံးပုဒ် ဖန်တီးပြီး `filter` နဲ့ 180 စက္ကန့်ထက် ရှည်တာတွေကိုပဲ ထားပါ
  • ကျန်တဲ့ သီချင်းတစ်ပုဒ်ချင်းစီကို title နဲ့ မိနစ် (seconds / 60) နဲ့ print ထုတ်ပါ
  • `String?` type guest တစ်ခုကို null ပေးပြီး null ဆိုရင် `solo` လို့ print ထုတ်ပါ
Playlist.kt
// TODO: declare a data class Song with a title (String) and seconds (Int)

fun main() {
    // TODO: build a listOf three Song values
    // TODO: keep only the songs longer than 180 seconds (filter)
    // TODO: print each remaining song: title and whole minutes
    // TODO: a nullable String? guest set to null; print it, or "solo" when null
}

ရလဒ်: Monsoon (4 min) Echo (3 min) Guest: solo

အရိပ်အမြွက် ကြည့်ရန်

`filter { it.seconds > 180 }` နဲ့ စစ်ပါ။ Null ဖြစ်နိုင်တဲ့ value အတွက် Elvis operator `?:` ကို သုံးပါ: `guest ?: "solo"`။ Int ချင်း စားရင် ဒသမကို ဖြတ်ပစ်လို့ 245 / 60 က 4 ပါ။

အဖြေ ကြည့်ရန်
Playlist.kt
data class Song(val title: String, val seconds: Int)

fun main() {
    val playlist = listOf(Song("Dawn", 150), Song("Monsoon", 245), Song("Echo", 200))

    val longSongs = playlist.filter { it.seconds > 180 }
    for (song in longSongs) {
        println("${song.title} (${song.seconds / 60} min)")
    }

    val guest: String? = null
    println("Guest: ${guest ?: "solo"}")
}

Quick Quiz 1 / 3

Kotlin နဲ့ JVM ရဲ့ ဆက်ဆံရေးကို ဘယ်စကားက မှန်မှန်ကန်ကန် ဖော်ပြလဲ။

Quick Quiz 2 / 3

ဘယ် declaration ကို Kotlin compiler က ငြင်းပယ်မလဲ။

Quick Quiz 3 / 3

`val subtitle: String? = null` ဖြစ်နေတဲ့အခါ `println(subtitle?.length ?: 0)` က ဘာ print ထုတ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Kotlin က language ဖြစ်ပြီး JVM က bytecode ကို run ပေးတဲ့ platform ပါ၊ နှစ်ခုကို ခွဲမြင်ပါ။
  • ရည်မှန်းချက်လေးခုက concise, safe, interoperable, tool-friendly ဖြစ်ပါတယ်။
  • `String` က null မဖြစ်နိုင်ပြီး `String?` က ဖြစ်နိုင်ပါတယ်၊ compiler က ခွဲစစ်ပေးပါတယ်။
  • Kotlin က Java library တွေကို တိုက်ရိုက်သုံးနိုင်ပေမယ့် Java က ပြန်တဲ့ value တွေမှာ null crash ပြန်ဖြစ်နိုင်သေးပါတယ်။

Kotlin releases — Kotlin

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

What is Kotlin? | Thuta Learning