ရိုးရိုးလေး ရှင်းပြမယ်
Kotlin ကို ရိုးရိုးလေး ပြောရရင်
Kotlin ကို တင်းကျပ်ပေမယ့် ကူညီတတ်တဲ့ editor တစ်ယောက်လို မြင်ကြည့်ပါ။ ကွန်ပျူတာကို လုပ်ခိုင်းချင်တာတွေကို တိုတိုရှင်းရှင်း line တွေနဲ့ ရေးလိုက်ပါတယ်။ ဘာမှ မ run ခင် Kotlin compiler က အဲဒီ line တွေကို စစ်ပါတယ် — type မှားနေလား၊ မရှိတဲ့ နာမည်ကို သုံးထားလား၊ ပျောက်နေနိုင်တဲ့ value ကို မစစ်ဘဲ သုံးထားလား ဆိုတာတွေပါ။ စစ်ဆေးမှုကို ကျော်တဲ့ code ပဲ run နေတဲ့ program ဖြစ်လာပါတယ်။
ဒီ language တစ်ခုတည်းကိုပဲ phone app၊ web back end၊ command-line tool နဲ့ platform အမျိုးမျိုးမှာ ဝေမျှသုံးတဲ့ code တွေအတွက် သုံးကြပါတယ်။ ဒါကြောင့် ဒီ course မှာ သင်တဲ့ အခြေခံတွေက ဘယ်နေရာမှာ သုံးသုံး အလုပ်ဖြစ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: မြေပုံမရှိဘဲ စလိုက်ရတာ
Kotlin tutorial တွေကို search လုပ်ကြည့်ရင် ပထမဆုံး ရလဒ်တွေက Android layout၊ Gradle plugin၊ coroutine နဲ့ Spring controller တွေအကြောင်း တစ်ပြိုင်တည်း ပြောနေတတ်ပါတယ်။ Beginner တစ်ယောက်က အဲဒီထဲက တစ်ခုကို တန်းဝင်လိုက်ရင် screen ပထမစာမျက်နှာမှာတင် မရင်းနှီးတဲ့ အယူအဆ ဆယ်ခုလောက်နဲ့ တွေ့ပါတယ်။ Error တစ်ခုက ဘယ် tool ကနေ လာတာလဲ ခွဲမသိနိုင်တော့ဘဲ Kotlin ကိုယ်တိုင်က ခက်တယ်လို့ ယူဆပြီး ရပ်သွားတတ်ပါတယ်။ တကယ်တော့ ခက်တာက language မဟုတ်ဘဲ အစီအစဉ် ပျောက်နေတာပါ။
အစီအစဉ်က ဘာကြောင့် အရေးကြီးလဲ
နောက်ပိုင်း chapter တိုင်းက အရင် chapter တွေကို ပြန်သုံးပါတယ်။ Null safety က type ဆိုတာ ဘာလဲ သိပြီးသားလို့ ယူဆပါတယ်၊ lambda က function ရေးတတ်ပြီးသားလို့ ယူဆပါတယ်၊ coroutine ကတော့ နှစ်ခုလုံးကို ယူဆပါတယ်။ Course မြေပုံကို အရင်မြင်ထားရင် နောက်ပိုင်း အယူအဆတစ်ခုစီ ဘယ်နေရာမှာ ကပ်ရမလဲ သိနေပါလိမ့်မယ်။
အဓိက သဘောတရား: language တစ်ခု၊ ကူညီတဲ့ compiler၊ run လို့ရတဲ့ နေရာအများကြီး
Kotlin ဆိုတာ IntelliJ IDEA ကို ဖန်တီးခဲ့တဲ့ JetBrains ကုမ္ပဏီက ဖန်တီးထားတဲ့ statically typed ဖြစ်ပြီး general-purpose သုံးလို့ရတဲ့ programming language တစ်ခုပါ။ "Statically typed" ဆိုတာ value တိုင်းမှာ code ကို compile လုပ်တဲ့အချိန်မှာတင် သိထားတဲ့ type ရှိတယ်လို့ ဆိုလိုပါတယ် — ဂဏန်းပြည့်အတွက် `Int`၊ စာသားအတွက် `String`၊ true/false အတွက် `Boolean` ပေါ့။ Compiler က type တွေကို သိနေတဲ့အတွက် ဂဏန်းတစ်ခုကို စာသားနဲ့ ပေါင်းမိတဲ့ line မျိုးကို program မစခင်ကတည်းက ပယ်ချပေးနိုင်ပါတယ်။ ဘာသာစကားက open source ဖြစ်ပြီး syntax က ကျစ်လစ်တာကြောင့် အရင်ခေတ် language တွေမှာ line များများ ရေးရတဲ့ boilerplate တွေကို Kotlin မှာ line တစ်ကြောင်းတည်းနဲ့ ရေးလို့ရတာ များပါတယ်။
Kotlin source file တွေရဲ့ extension က `.kt` ပါ။ Compiler `kotlinc` က အဲဒီ file တွေကို Java program တွေ compile ဖြစ်တဲ့ format အတူတူဖြစ်တဲ့ JVM bytecode အဖြစ် ပြောင်းပေးပါတယ်။ ဒါကြောင့် Kotlin program တစ်ခုက Java Virtual Machine ရှိတဲ့ နေရာတိုင်းမှာ run နိုင်ပြီး Java library ဘာမဆို ခေါ်သုံးနိုင်သလို project တစ်ခုတည်းထဲမှာ Java code ကလည်း Kotlin code ကို ပြန်ခေါ်နိုင်ပါတယ်။ ဒီ language ကိုပဲ Android၊ JavaScript နဲ့ native machine code အတွက်လည်း compile လုပ်လို့ရပြီး Kotlin Multiplatform က codebase တစ်ခုတည်းက logic ကို target အများအပြားမှာ ဝေမျှသုံးခွင့် ပေးပါတယ်။
1. ရေးပါ
ညွှန်ကြားချက်တွေကို `Welcome.kt` လို file တစ်ခုထဲ သိမ်းပါတယ်။ Editor ကလည်း ရေးနေတုန်းမှာပဲ မျဉ်းနီနဲ့ သတိပေးပါတယ်။
2. စစ်ပါ
`kotlinc` က file ကို ဖတ်ပြီး နာမည်တိုင်းနဲ့ type တိုင်းကို စစ်ပါတယ်။ စာလုံးပေါင်းမှားတာ ဒါမှမဟုတ် type မကိုက်တာ ရှိရင် build က ဒီအဆင့်မှာတင် ရပ်သွားပြီး file၊ line နဲ့ column ကို ပြပါတယ်။
3. Compile လုပ်ပါ
စစ်ဆေးမှုတွေ ကျော်ရင် compiler က bytecode (class file တွေ၊ များသောအားဖြင့် jar တစ်ခုထဲ စုထားတာ) ကို ရေးပေးပါတယ်။
4. Run ပါ
JVM က `main` ကို ရှာပြီး bytecode ကို အပေါ်ကနေ အောက်ကို run ပါတယ်။ `println` ရဲ့ စာသားတွေ terminal မှာ ပေါ်လာပါတယ်။
စပြီး ကြည့်လိုက်ရင် Kotlin မှာ စတင်သူ language အများစုနဲ့ ကွဲတဲ့ အကျင့်အချို့ကို တွေ့ပါမယ်။ Statement တိုင်းက line ဆုံးတဲ့နေရာမှာ ပြီးသွားလို့ semicolon က မလိုပါဘူး။ Value တွေကို `val` (ဘယ်တော့မှ မပြောင်းတဲ့ နာမည်) ဒါမှမဟုတ် `var` (နောက်မှ ပြောင်းလို့ရတဲ့ နာမည်) နဲ့ ကြေညာပါတယ်။ စာသားထဲကို `$name` လို string template နဲ့ value တွေ တိုက်ရိုက် ထည့်လို့ရပါတယ်။ ပြီးတော့ value တစ်ခု ကျန်ခဲ့နိုင်တာ (null) ကိုလည်း compiler က ခြေရာခံပေးပါတယ်၊ ဒါကို null safety လို့ခေါ်ပြီး ဒီ chapter ရဲ့ နောက်ပိုင်း lesson တွေမှာ သီးသန့် လေ့လာရပါမယ်။
ဒီ course ကို Kotlin 2.x အတွက် ရေးထားပါတယ်။ Run လို့ရတဲ့ example တိုင်းက top-level `fun main()` ပါတဲ့ file တစ်ခုတည်း ဖြစ်လို့ Kotlin Playground ထဲ paste လုပ်လို့ရသလို Get Started lesson မှာ ပြထားသလို ကိုယ်တိုင် compile လုပ်လို့လည်း ရပါတယ်။ လိုက်လေ့လာဖို့ Android Studio၊ framework တစ်ခုခု ဒါမှမဟုတ် Kotlin အတွေ့အကြုံ ဘာမှ မလိုပါဘူး။
- .kt file ထဲမှာ Kotlin ရေးတယ်
- kotlinc က type နဲ့ နာမည် စစ်တယ်
- Bytecode ထွက်လာတယ်
- JVM က main() ကို run တယ်
Compiler က သင့် code ကို ပထမဆုံး ဖတ်တဲ့ တင်းကျပ်တဲ့ စာဖတ်သူပါ။ Beginner အမှားအများစုကို နှစ်ခုမြောက် အဆင့်မှာတင် ဖမ်းမိလို့ ဘာမှ မ run ခင်ကတည်း သိရပါတယ်။
အထွေထွေ ပုံစံ: Kotlin program အသေးလေး
fun main() {
val name = "some text"
println("Text that mentions $name")
}`main` ရဲ့ ကွင်းကောက် `{ }` ထဲက line တွေကို အပေါ်ကနေ အောက်ကို တစ်ကြောင်းချင်း run ပါတယ်။ `val` နဲ့ နာမည်တစ်ခုကို value တစ်ခုနဲ့ ချိတ်ပြီး `println` က စာသားကို print ထုတ်ပါတယ်။ Line ဆုံးမှာ semicolon မလိုပါဘူး။
ပထမဆုံး Kotlin program
ဒီ program က အချက်အလက်နှစ်ခုကို နာမည်ပေးထားတဲ့ value တွေအဖြစ် သိမ်းပြီး အဲဒီ value တွေနဲ့ စာကြောင်းသုံးကြောင်း ဆောက်ကာ print ထုတ်ပါတယ်။ Run ပြီးရင် `finished` ကို တခြားနံပါတ်တစ်ခု ပြောင်းပြီး ထပ် run ကြည့်ပါ။ Output ကို ဘာကြောင့် ပြောင်းသွားလဲ ကိုယ်တိုင် ရှင်းပြနိုင်ရင် ဒီ lesson က ရည်ရွယ်ချက် ပြည့်ပါပြီ။
fun main() {
val course = "Kotlin"
val lessons = 33
val finished = 1
println("Welcome to the $course course!")
println("You have finished $finished of $lessons lessons.")
println("Lessons left: ${lessons - finished}")
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- The entry point
fun main() {`fun` နဲ့ function တစ်ခု ကြေညာပြီး `main` လို့ နာမည်ပေးထားတဲ့ function ကနေ program စ run ပါတယ်။ ကွင်းကောက် `{ }` က run မယ့် line တွေကို ဘောင်ခတ်ပေးပါတယ်။
- A text value
val course = "Kotlin"`val` က `course` ဆိုတဲ့ နာမည်ကို `"Kotlin"` နဲ့ ချိတ်ပါတယ်။ စာသားကို double quote နဲ့ ရေးပြီး Kotlin က ဒီ value ရဲ့ type ကို `String` လို့ ကိုယ်တိုင် သိပါတယ် — ရေးပေးစရာ မလိုပါဘူး။
- A whole number
val lessons = 33ဂဏန်းပြည့်တွေရဲ့ type က `Int` ပါ။ ဒီ line မှာလည်း semicolon မပါပါဘူး၊ line ဆုံးတာနဲ့ statement ပြီးပါတယ်။
- Inserting a value into text
println("Welcome to the $course course!")`println` က စာသားကို print ထုတ်ပြီး နောက်ထပ် line အသစ် တစ်ကြောင်း ဆင်းပေးပါတယ်။ စာသားထဲက `$course` နေရာမှာ `course` ရဲ့ value ဖြစ်တဲ့ Kotlin ကို အစားထိုးပေးပါတယ်။
- An expression inside a template
println("Lessons left: ${lessons - finished}")နာမည်တစ်ခုထက် ပိုရှုပ်တဲ့ အရာကို ထည့်ချင်ရင် `${ }` နဲ့ ဖွင့်ပါတယ်။ `lessons - finished` ကို တွက်ပြီး 32 ကို စာသားထဲ ထည့်ပေးပါတယ်။
Welcome to the Kotlin course!
You have finished 1 of 33 lessons.
Lessons left: 32- Basic: syntax, type, control flow, null safety
- Intermediate: function, collection, class
- Advanced: lambda, generic, coroutine, tooling
- Projects နဲ့ Exercises: ကိုယ်တိုင် တည်ဆောက်ပြီး လေ့ကျင့်
Lesson 33 ခုကို ဘယ်လို ခွဲထားလဲ
Course ကို အပိုင်းငါးပိုင်း ခွဲထားပြီး တစ်ပိုင်းချင်းစီက အရင်အပိုင်းပေါ်မှာ ဆက်တည်ဆောက်ပါတယ်။ အစကနေ အစီအစဉ်အတိုင်း သွားဖို့ အကြံပြုပါတယ်၊ ဘာကြောင့်လဲဆိုတော့ Advanced ထဲက lambda နဲ့ coroutine တွေက Basic နဲ့ Intermediate မှာ သင်တဲ့ အရာတွေကို ပေါင်းစပ်သုံးထားလို့ပါ။
| အပိုင်း | ပါဝင်တာတွေ |
|---|---|
| Basic (10) | Home, intro, setup, syntax, val နဲ့ var, data type, if နဲ့ when, loop, null safety, safe call နဲ့ Elvis operator |
| Intermediate (8) | Function, list, map, class နဲ့ object, constructor, inheritance, interface, data class |
| Advanced (10) | Lambda, higher-order function, generic နဲ့ extension, sealed type နဲ့ Result, mini project, coroutine, Flow, Gradle, testing, Multiplatform |
| Projects (3) | Task Manager တစ်ခုကို setup၊ features၊ polish ဆိုပြီး အပိုင်းသုံးပိုင်းခွဲ တည်ဆောက်တာ |
| Exercises (2) | Fundamentals အတွက် practice set တစ်ခု၊ OOP နဲ့ collection အတွက် တစ်ခု |
Lesson တစ်ခုချင်းကို ဘယ်လို လေ့လာမလဲ
Lesson တိုင်းက အစီအစဉ်တူပါတယ်: ရိုးရိုးရှင်းပြချက်၊ ပြဿနာတစ်ခု၊ အဓိက သဘောတရား၊ ယေဘုယျပုံစံ၊ line တစ်ကြောင်းချင်း ရှင်းပြထားတဲ့ example၊ အမှားတွေ၊ mini challenge နဲ့ quiz သုံးခုပါ။ ဖတ်ရုံနဲ့ မလုံလောက်ပါဘူး — Kotlin ရဲ့ compiler message တွေက အကောင်းဆုံး ဆရာတွေမို့ ကိုယ်တိုင် ရေးပြီး ကိုယ်တိုင် ချိုးကြည့်မှ မှတ်မိပါတယ်။
1. ကြိုမှန်းပါ
Example ကို မ run ခင် output ဘာထွက်မလဲ စာရွက်ပေါ် ရေးထားပါ။
2. Run ပါ
Playground ထဲမှာပဲ ဖြစ်ဖြစ်၊ ကိုယ့်စက်မှာပဲ ဖြစ်ဖြစ် ကိုယ်တိုင် ရိုက်ပြီး run ပါ။ Copy လုပ်တာထက် ရိုက်တာက မျက်စိ၊ လက်၊ စိတ် သုံးမျိုးလုံး အလုပ်လုပ်စေပါတယ်။
3. တစ်ခုပြောင်းပါ
Value တစ်ခု၊ နာမည်တစ်ခု ဒါမှမဟုတ် type တစ်ခုကို ပြောင်းပြီး output ကို ထပ်မှန်းကြည့်ပါ။
4. တမင် ချိုးကြည့်ပါ
စာလုံးတစ်လုံး မှားရေးပြီး compiler message ကို ဖတ်ပါ။ File၊ line နဲ့ အမှားအမျိုးအစားကို ဖတ်တတ်လာရင် နောက်ပိုင်း အမှားတွေ ပြင်ရတာ မြန်လာပါမယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့မှာ Kotlin ကို ဘယ်နေရာတွေမှာ သုံးလဲ
Android က Kotlin ကို first-class language အဖြစ် ထောက်ပံ့ပြီး app အသစ်တွေကို Kotlin နဲ့ ရေးတာ အလွန်အများအပြားပါ။ Server ဘက်မှာလည်း Ktor နဲ့ Spring Boot လို framework တွေကို Kotlin နဲ့ သုံးနိုင်ပြီး Java library တွေ အကုန် ရှိပြီးသားမို့ အသစ်ပြန်ရေးစရာ မလိုပါဘူး။ Gradle build file တွေကိုလည်း `build.gradle.kts` ဆိုပြီး Kotlin နဲ့ ရေးလို့ရလို့ project တစ်ခုကို build လုပ်တဲ့ script ကိုယ်တိုင်က Kotlin ဖြစ်နေတတ်ပါတယ်။
Kotlin Multiplatform ကိုတော့ validation၊ networking လိုမျိုး business logic တွေကို app တစ်ခုထက်ပိုတဲ့ platform တွေကြား ဝေမျှသုံးဖို့ အသုံးပြုကြပါတယ်။ Command-line tool နဲ့ data processing script လေးတွေအတွက်လည်း Kotlin က သင့်တော်ပါတယ်၊ ဘာကြောင့်လဲဆိုတော့ code တိုပြီး compiler က အမှားတွေကို အရင်ဖမ်းပေးလို့ပါ။ ဒီ course မှာ လေ့လာမယ့် အခြေခံတွေက အဲဒီနေရာတွေအားလုံးမှာ အတူတူပါပဲ — Android layout နဲ့ Spring annotation တွေက language ရဲ့ အပေါ်မှာ ထပ်တင်ထားတဲ့ အရာတွေသာ ဖြစ်ပါတယ်။
အမှားများ
အစပိုင်းမှာ မကြာခဏ ဖြစ်တတ်တဲ့ အမှားများ
1. Kotlin က Android အတွက်ပဲလို့ ထင်တာ
Android tutorial ကို တန်းစလိုက်ရင် Android SDK က ပေးတဲ့ အရာနဲ့ Kotlin language ကိုယ်တိုင်က ပေးတဲ့ အရာကို ခွဲမသိနိုင်တော့ပါဘူး။ ဒီ course ရဲ့ ပထမ chapter တွေကို `fun main()` ပဲ ပါတဲ့ file သက်သက်နဲ့ လေ့လာပါ၊ ပြီးမှ framework တစ်ခုခုကို ရွေးပါ။
2. ရေးမကြည့်ဘဲ ဖတ်ရုံပဲ ဖတ်တာ
Output ကို မှန်းထားပြီး run မကြည့်ရင် မှန်လားမှားလား မသိနိုင်ပါဘူး။ Example တိုင်းကို ရိုက်ပြီး run ပါ၊ ပြီးတော့ `println` ကို `printn` လို့ စာလုံးပေါင်းမှားကြည့်တာ ဒါမှမဟုတ် ပိတ်ကွင်းတစ်ခု ဖျက်ကြည့်တာတွေ လုပ်ပြီး compiler message ကို ဖတ်ပါ။ Compiler message ကို ကိုယ်တိုင် မြင်ဖူးမှ နောက်တစ်ခါ တွေ့ရင် မှတ်မိပါမယ်။
3. Coroutine နဲ့ lambda ကို အရင် ကျော်သွားတာ
နောက်ပိုင်း chapter တွေက `val`၊ type၊ null safety နဲ့ function ကို ကျွမ်းကျင်ပြီးသားလို့ ယူဆပါတယ်။ အဲဒါတွေ မခိုင်ခင် ကျော်သွားရင် error message တစ်ခုချင်းက ရှင်းရခက်လာပြီး ပြန်ကျော့ရတာ ပိုကြာပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Scratch file တစ်ခု ဖန်တီးထားပြီး lesson တစ်ခုချင်းရဲ့ ကိုယ်ပိုင် စမ်းသပ်မှုတွေကို အဲဒီထဲမှာ ထားပါ။
- Compiler error တစ်ခု ပေါ်တိုင်း message ရဲ့ ပထမ line ကို စာအုပ်ထဲ မှတ်ထားပါ၊ ကြာလာရင် ကိုယ်ပိုင် အမှားစာရင်း ဖြစ်လာပါလိမ့်မယ်။
- Warning တွေကိုလည်း မကျော်ပါနဲ့၊ compiler က ပိုကောင်းတဲ့ ရေးနည်း ညွှန်ပြနေတာ ဖြစ်တတ်ပါတယ်။
Mini challenge
Mini challenge: လေ့လာမယ့် အစီအစဉ်
ကိုယ့်အမြန်နှုန်းအတိုင်း course တစ်ခုလုံး ပြီးဖို့ ဘယ်နှရက် လိုမလဲ တွက်ပေးတဲ့ program ရေးပါ။
- နာမည်၊ lesson စုစုပေါင်း (33) နဲ့ တစ်ရက်ကို လေ့လာမယ့် lesson အရေအတွက်ကို `val` သုံးခုနဲ့ သိမ်းပါ
- ရက်အရေအတွက်ကို အပေါ်ဘက် ဝိုင်းပြီး တွက်ပါ — တစ်ရက် 4 ခုနှုန်းဆို 33 lesson က 8 ရက် မဟုတ်ဘဲ 9 ရက် ယူပါတယ်
- String template သုံးပြီး စာကြောင်းတစ်ကြောင်း print ထုတ်ပါ
- `lessonsPerDay` ကို ပြောင်း၊ run၊ ရလဒ်ကို လက်နဲ့ တွက်စစ်ကြည့်ပါ
fun main() {
// TODO: three vals: your name, the total number of lessons, lessons per day
// TODO: days needed, rounded UP (whole-number division drops the remainder)
// TODO: print one sentence that mentions the name and all three numbers
}ရလဒ်: Aye needs 9 days for 33 lessons at 4 a day.
အရိပ်အမြွက် ကြည့်ရန်
Int ချင်း စားရင် အကြွင်းကို ဖြတ်ပစ်ပါတယ် — 33 / 4 က 8 ပါ။ အပေါ်ဘက် ဝိုင်းချင်ရင် စားမယ့်သူရဲ့ (စားကိန်း - 1) ကို အရင် ပေါင်းထည့်ပါ: (33 + 3) / 4 = 9 ။
အဖြေ ကြည့်ရန်
fun main() {
val name = "Aye"
val totalLessons = 33
val lessonsPerDay = 4
val days = (totalLessons + lessonsPerDay - 1) / lessonsPerDay
println("$name needs $days days for $totalLessons lessons at $lessonsPerDay a day.")
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Kotlin ဆိုတာ JetBrains ဖန်တီးထားတဲ့ statically typed language ဖြစ်ပြီး JVM bytecode အဖြစ် compile လုပ်ကာ Android၊ backend နဲ့ multiplatform အတွက် သုံးပါတယ်။
- Compiler က code မ run ခင် နာမည်နဲ့ type တွေကို စစ်ပေးလို့ beginner အမှားအများစုကို စောစောသိရပါတယ်။
- Course က Basic 10၊ Intermediate 8၊ Advanced 10၊ Projects 3၊ Exercises 2 ဆိုပြီး lesson 33 ခု ရှိပြီး အစီအစဉ်အတိုင်း လေ့လာသင့်ပါတယ်။
- Example တိုင်းကို ကြိုမှန်း၊ ရိုက်ပြီး run၊ တစ်ခုပြောင်း၊ တမင်ချိုးကြည့် ဆိုတဲ့ routine နဲ့ လေ့လာပါ။
Kotlin releases — Kotlin