ရိုးရိုးလေး ရှင်းပြမယ်
Data type ကို ရိုးရိုးလေး ပြောရရင်
Data type ဆိုတာ value တစ်ခုက ဘာအမျိုးအစားလဲ၊ သူနဲ့ ဘာတွေ လုပ်လို့ရလဲ ပြောတဲ့ တံဆိပ်ပါ။ ဆိုင်မှာ ပစ္စည်းတစ်မျိုးစီအတွက် တိုင်းတာတဲ့ ကိရိယာ မတူသလိုပါပဲ — အရှည်ကို ပေတံနဲ့ တိုင်းပြီး အလေးချိန်ကို ချိန်ခွင်နဲ့ ချိန်ပါတယ်။ Kotlin မှာလည်း ဂဏန်းပြည့်၊ ဒသမ ဂဏန်း၊ စာလုံးတစ်လုံး၊ true/false နဲ့ စာသား ဆိုပြီး type တစ်မျိုးစီ ရှိပါတယ်။
Kotlin က type တွေကို တင်းကျပ်ပါတယ်။ ဂဏန်းတစ်မျိုးကို တခြားတစ်မျိုးအဖြစ် တိတ်တိတ်ပါးပါး မပြောင်းပေးပါဘူး၊ ပြောင်းချင်ရင် ကိုယ်တိုင် ပြောရပါတယ်။ အဲဒါက ရှုပ်ရှက်ခတ်သလို ထင်ရပေမယ့် ကိုယ်မသိဘဲ တန်ဖိုးပျက်သွားတာမျိုးကို ကာကွယ်ပေးတာပါ။
Problem → Why → Solution
ပြဿနာ: crash မဖြစ်ဘဲ မှားတဲ့ အဖြေပေးတဲ့ ဂဏန်းတွေ
File size တွက်တဲ့ program ကို ကြည့်ပါ။ `kilobytes` က 3,000,000 ဖြစ်ပြီး 1024 နဲ့ မြှောက်တာကို မျှော်လင့်ထားတာက 3,072,000,000 ပါ။ ဒါပေမယ့် program က အနှုတ်ဂဏန်း `-1222967296` ကို print ထုတ်ပါတယ်။ ဘာ error၊ ဘာ warning မှ မပြပါဘူး။ ဒုတိယ ပြဿနာက ပိုသေးငယ်ပေမယ့် ပိုအဖြစ်များပါတယ်: 7 + 8 ကို 2 နဲ့ စားတော့ 7.5 မဟုတ်ဘဲ 7 ထွက်ပါတယ်။
fun main() {
val kilobytes = 3_000_000
val bytes = kilobytes * 1024
println("Bytes: $bytes") // Bytes: -1222967296
val total = 7 + 8
println("Average: ${total / 2}") // Average: 7
}ဒီလို ဖြစ်ရတဲ့ အကြောင်းရင်း
ဂဏန်းပြည့်ကို Kotlin က `Int` လို့ ယူပြီး `Int` က bit 32 ခုပဲ ရှိလို့ −2,147,483,648 ကနေ 2,147,483,647 အထိပဲ သိမ်းနိုင်ပါတယ်။ ဒီထက် ကျော်သွားရင် error မပေးဘဲ ဟိုဘက်စွန်းကို ပြန်ရောက်သွား (wrap) ပါတယ်။ `Int` ကို `Int` နဲ့ စားတာကလည်း ကိန်းပြည့်စားခြင်းမို့ ဒသမကို ဖြတ်ပစ်ပါတယ်။ ဖြေရှင်းနည်းက ပိုကျယ်တဲ့ `Long` ဒါမှမဟုတ် ဒသမပါတဲ့ `Double` ကို ရွေးဖို့ပါ။
အဓိက သဘောတရား: type တွေ၊ conversion နဲ့ ကန့်သတ်ချက်တွေ
ဂဏန်း type တွေကို အရွယ်အစားနဲ့ ခွဲထားပါတယ်။ ကိန်းပြည့်အတွက် `Byte`၊ `Short`၊ `Int`၊ `Long` နဲ့ ဒသမအတွက် `Float`၊ `Double` ရှိပါတယ်။ ကိုယ်တိုင် ရွေးစရာ မလိုရင် `Int` နဲ့ `Double` ကို သုံးပါ၊ ဘာကြောင့်လဲဆိုတော့ Kotlin က literal `42` ကို `Int`၊ `3.14` ကို `Double` လို့ အလိုအလျောက် ယူလို့ပါ။ ပိုကြီးတဲ့ ဂဏန်းအတွက် `5L` လို `L` နောက်ဆက်ပေါင်းပြီး `Long` လုပ်ပါတယ်၊ `Int` ထက်ကြီးတဲ့ literal ကို Kotlin က `Long` အဖြစ် ကိုယ်တိုင် ယူပါတယ်။ `Float` ကိုတော့ `2.5f` လို `f` နဲ့ ရေးပါတယ်။ `1_000_000` လို underscore ကို ဂဏန်းဖတ်ရလွယ်အောင် ထည့်ရေးလို့ရပါတယ်။
| Type | ကန့်သတ်ချက် | ဘာအတွက် သုံးလဲ |
|---|---|---|
| Byte | 8-bit၊ −128 မှ 127 | Raw data၊ file byte တွေ |
| Short | 16-bit၊ −32,768 မှ 32,767 | အသုံးနည်းတယ် |
| Int | 32-bit၊ အကြမ်းဖျင်း ±2.1 billion | ကိန်းပြည့် default၊ count နဲ့ index |
| Long | 64-bit၊ အကြမ်းဖျင်း ±9.2 × 10^18 | File size၊ timestamp၊ ID၊ ကြီးတဲ့ စုစုပေါင်း |
| Float | 32-bit၊ ဂဏန်း ၆ ကနေ ၇ လုံးအထိ တိကျ | Graphics၊ သိမ်းစရာနေရာ ကန့်သတ်တဲ့အခါ |
| Double | 64-bit၊ ဂဏန်း ၁၅ လုံးလောက်အထိ တိကျ | ဒသမ default၊ တိုင်းတာမှု တန်ဖိုးတွေ |
အရေးကြီးဆုံး စည်းမျဉ်းက implicit widening မရှိတာပါ။ `Int` value ကို `Long` variable ထဲ တိုက်ရိုက် ထည့်လို့ မရပါဘူး — `val total: Long = count` (`count` က Int) ဆိုရင် `error: initializer type mismatch: expected 'Long', actual 'Int'.` ပေါ်ပါတယ်။ `toLong()`, `toInt()`, `toDouble()` နဲ့ ကိုယ်တိုင် ပြောင်းရပါတယ်။ ဂဏန်းနှစ်မျိုး ရောတွက်တာတော့ လက်ခံပါတယ်၊ `1 + 2L` က `Long` ဖြစ်ပါတယ်။ ဒါပေမယ့် `count == 5L` လို နှိုင်းယှဉ်တာက `error: operator '==' cannot be applied to 'Int' and 'Long'.` ပေါ်ပါတယ်။
ပိုသေးတဲ့ type ကို ပြောင်းတဲ့အခါ အချက်အလက် ဆုံးရှုံးနိုင်ပါတယ်။ `3.99.toInt()` က 3 ကို ပေးပါတယ် (ဒသမကို ဖြတ်ပစ်တာ၊ round မလုပ်ပါဘူး)။ `300.toByte()` က 44 ကို ပေးပါတယ် (ပိုလျှံတာတွေ wrap ဖြစ်လို့)။ ကိန်းပြည့်ရဲ့ ကန့်သတ်ချက်ကို ကျော်ရင် `Int.MAX_VALUE + 1` က `-2147483648` ဖြစ်ပါတယ်၊ exception မထွက်ပါဘူး။ Overflow ကို ဖမ်းချင်ရင် Java ရဲ့ `Math.addExact(a, b)` ကို သုံးပါ၊ ကျော်ရင် `ArithmeticException: integer overflow` ပေးပါတယ်။ `7 / 2` က 3၊ `7 / 2.0` က 3.5၊ `7 % 2` က 1၊ `-7 / 2` က -3 ပါ (သုညဘက်ကို ဖြတ်ပါတယ်)။
`Char` က Unicode စာလုံးတစ်လုံးဖြစ်ပြီး single quote နဲ့ ရေးပါတယ်: `'K'`။ `'c' + 1` က `'d'`၊ `'c' - 'a'` က Int 2၊ `'c'.code` က 99 ပါ။ `Boolean` က `true` ဒါမှမဟုတ် `false` ပဲ ဖြစ်ပြီး `&&` (နှစ်ခုလုံးမှန်), `||` (တစ်ခုမှန်), `!` (ပြောင်းပြန်) နဲ့ တွဲပါတယ်။ `String` ကတော့ စာလုံးတွေ အစဉ်လိုက်ပါ၊ ပြောင်းလို့မရပါဘူး (immutable)။ `word[0]` က `Char` ပြန်ပေးပြီး `word[0] = 'k'` လို့ ရေးရင် `error: no 'set' operator method providing array access.` ပေါ်ပါတယ်။
- Value တိုင်းမှာ type တစ်ခု ပုံသေရှိတယ်
- Type တွေ တိတ်တိတ် မပြောင်းဘူး
- ပြောင်းချင်ရင် toLong(), toInt() သုံး
- Overflow က error မပေးဘဲ wrap ဖြစ်တယ်
Kotlin က type မကိုက်တာကို ရေးနေတုန်းမှာ ဖမ်းပေးပေမယ့် ဂဏန်းတန်ဖိုး ကျော်လွန်တာကိုတော့ run time မှာတောင် ဖမ်းမပေးပါဘူး။
အထွေထွေ ပုံစံ: type တွေနဲ့ conversion
val whole: Int = 42
val big: Long = 5_000_000_000L
val decimal: Double = 3.14
val letter: Char = 'K'
val flag: Boolean = true
val text: String = "Kotlin"
val widened: Long = whole.toLong()Type ကို နာမည်နောက်မှာ `: Type` နဲ့ ရေးပါတယ်။ ရေးမပေးရင်လည်း value ကနေ Kotlin က ခန့်မှန်းပါတယ်။ `Long` literal က `L`၊ `Char` က single quote၊ `String` က double quote ပါ။ နောက်ဆုံး line က `Int` ကို `Long` အဖြစ် ကိုယ်တိုင် ပြောင်းတာပါ။
စာအုပ်တစ်အုပ်ရဲ့ အချက်အလက်တွေ
ဒီ program က စာအုပ်တစ်အုပ်ရဲ့ အချက်အလက်ကို type အမျိုးမျိုးနဲ့ သိမ်းပြီး conversion သုံးမျိုးကို ပြပါတယ်: `Int` ကနေ `Long`၊ `Double` ကနေ `Int`၊ `Char` ပေါင်းခြင်း။ နောက်ဆုံး line မှာ overflow ကိုလည်း မျက်စိနဲ့ မြင်ရပါလိမ့်မယ်။ Output ကို အရင်ကြိုမှန်းပြီးမှ run ကြည့်ပါ။
fun main() {
val title: String = "Kotlin Basics"
val pages: Int = 320
val price: Double = 19.99
val grade: Char = 'A'
val inStock: Boolean = true
val copies: Long = pages.toLong() * 1_000_000
val dollars: Int = price.toInt()
val nextGrade: Char = grade + 1
println("$title: $pages pages, price $price, grade $grade, in stock: $inStock")
println("Copies: $copies, whole dollars: $dollars, next grade: $nextGrade")
println("Int.MAX_VALUE + 1 = ${Int.MAX_VALUE + 1}")
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Explicit types
val price: Double = 19.99`: Double` ကို ရေးထားပေမယ့် မလိုပါဘူး — `19.99` ကနေ Kotlin က `Double` လို့ သိပါတယ်။ ဒီ program မှာ type အားလုံးကို ရည်ရွယ်ချက်ရှိရှိ ရေးပြထားတာပါ။ Double ကို `val price: Double = 20` လို ကိန်းပြည့်နဲ့ ပေးလို့တော့ မရပါဘူး၊ `20.0` လို့ ရေးရပါတယ်။
- Int widened to Long
val copies: Long = pages.toLong() * 1_000_000`pages.toLong()` က 320 ကို `Long` အဖြစ် အရင်ပြောင်းပြီးမှ မြှောက်ပါတယ်။ `Long * Int` ရလဒ်က `Long` ဖြစ်လို့ `copies: Long` နဲ့ ကိုက်ပါတယ်။ `1_000_000` ထဲက underscore က ဖတ်ရလွယ်အောင်သာ ထည့်တာပါ။
- Double narrowed to Int
val dollars: Int = price.toInt()`19.99.toInt()` က ဒသမကို ဖြတ်ပစ်လို့ 19 ရပါတယ်၊ 20 မဟုတ်ပါဘူး။ ဒီအတွက် Kotlin က ကိုယ်တိုင် `toInt()` ခေါ်ခိုင်းတာပါ — ဆုံးရှုံးမှုကို မြင်သာအောင်ပါ။ Round လုပ်ချင်ရင် `kotlin.math.roundToInt()` ကို သုံးပါ။
- Char arithmetic
val nextGrade: Char = grade + 1`Char + Int` ရလဒ်က `Char` ဖြစ်ပါတယ်၊ `'A'` ရဲ့ နောက်စာလုံး `'B'` ပါ။ `Char` နှစ်ခုကို ပေါင်းလို့တော့ မရပါဘူး (`'a' + 'b'` က `argument type mismatch` error ပေးပါတယ်)၊ ဒါပေမယ့် တစ်ခုကို တစ်ခုက နုတ်ရင် `Int` ရပါတယ်။
- Several values in one line
println("$title: $pages pages, price $price, grade $grade, in stock: $inStock")Template တစ်ခုထဲမှာ type အမျိုးမျိုးကို ရောထည့်လို့ရပါတယ်။ `Int` ကို `320`၊ `Double` ကို `19.99`၊ `Char` ကို `A`၊ `Boolean` ကို `true` လို့ ဖော်ပြပေးပါတယ်။
- Silent overflow
println("Int.MAX_VALUE + 1 = ${Int.MAX_VALUE + 1}")`Int.MAX_VALUE` က `Int` ရဲ့ အကြီးဆုံးတန်ဖိုး 2147483647 ပါ။ 1 ပေါင်းလိုက်တာနဲ့ အနိမ့်ဆုံးဘက်ကို wrap ဖြစ်ပြီး `-2147483648` ထွက်ပါတယ်။ Compiler က ဘာ warning၊ exception မှ မပေးပါဘူး။
Kotlin Basics: 320 pages, price 19.99, grade A, in stock: true
Copies: 320000000, whole dollars: 19, next grade: B
Int.MAX_VALUE + 1 = -2147483648ဂဏန်းတွက်ချက်မှုနဲ့ overflow ကို တစ်ဆင့်ချင်း ကြည့်ခြင်း
အောက်က program မှာ ဒီ lesson ရဲ့ ဂဏန်းစည်းမျဉ်းတွေကို တစ်ကြောင်းချင်း ပြထားပါတယ်။ Comment ထဲက ရလဒ်ကို မ run ခင် ဖုံးထားပြီး ကိုယ်တိုင် မှန်းကြည့်ပါ၊ ပြီးမှ run ပြီး တိုက်ကြည့်ပါ။ `big.toLong() + 1` က widen အရင်လုပ်ပြီးမှ ပေါင်းလို့ overflow မဖြစ်တာကို သတိထားပါ။
fun main() {
val big = 2_147_483_647
println(big + 1) // -2147483648 (wraps around)
println(big.toLong() + 1) // 2147483648 (widen first, then add)
println(7 / 2) // 3 (whole-number division)
println(7 / 2.0) // 3.5 (one Double makes it Double)
println(7 % 2) // 1 (remainder)
println(3.99.toInt()) // 3 (cuts the fraction, no rounding)
println(0.1 + 0.2) // 0.30000000000000004 (binary fractions)
println(1 + 2L) // 3 (Int + Long is Long)
}`0.1 + 0.2` က `0.3` မဟုတ်တာ Kotlin ရဲ့ ပြဿနာ မဟုတ်ပါဘူး။ ကွန်ပျူတာတွေက ဒသမကို binary နဲ့ သိမ်းတာမို့ `0.1` လို ဂဏန်းအချို့ကို အတိအကျ မသိမ်းနိုင်ပါဘူး။ ဒါကြောင့် `Double` နှစ်ခုကို `==` နဲ့ အတိအကျ နှိုင်းယှဉ်တာမျိုးကို ရှောင်ပါ၊ ငွေကြေးအတွက်လည်း `Double` မသုံးသင့်ပါဘူး။
Char၊ Boolean နဲ့ range
`Char` မှာ ကိန်းဂဏန်းတန်ဖိုး (Unicode code) တစ်ခု ရှိပြီး `.code` နဲ့ ယူလို့ရပါတယ်။ စာလုံးနှစ်လုံးကို နုတ်ရင် အကွာအဝေးကို `Int` အဖြစ် ရပါတယ်။ Range ကတော့ `1..10` (နှစ်ဖက်စလုံး ပါ)၊ `1 until 10` (အဆုံးမပါ) ဆိုပြီး `in` နဲ့ တွဲပြီး Boolean ရလဒ် ထုတ်ပေးပါတယ်။ Range ကို loop မှာ သုံးတာကို နောက် lesson တွေမှာ ကြည့်ပါမယ်။
fun main() {
val letter = 'c'
println(letter + 1) // d
println(letter - 'a') // 2
println(letter.code) // 99
println(letter in 'a'..'z') // true
val age = 17
val canVote = age >= 18
val isTeen = age in 13..19
println(canVote || isTeen) // true
println(!canVote && isTeen) // true
}String၊ buildString နဲ့ input parse လုပ်ခြင်း
String ကို ပြောင်းလို့ မရတာမို့ စာသားကို အပိုင်းပိုင်း ထပ်ပေါင်းနေရင် String အသစ်တွေ ထပ်ထပ်ဖြစ်နေပါတယ်။ ရှည်တဲ့ စာသားကို တည်ဆောက်ဖို့ `buildString { ... }` ကို သုံးပါ၊ ကွင်းကောက်ထဲမှာ `append(...)` နဲ့ အပိုင်းတွေ ထည့်ပြီး နောက်ဆုံး String တစ်ခု ပြန်ပေးပါတယ်။ User ဆီက ရတဲ့ စာသားကို ဂဏန်းပြောင်းတဲ့အခါ `"42".toInt()` က ဆိုးဆိုးရွားရွား ပျက်နိုင်လို့ (`"abc".toInt()` က exception ပေးပါတယ်) `toIntOrNull()` ကို သုံးပြီး မရရင် `null` ကို ယူပါ။
fun main() {
val word = "Kotlin"
println(word.length) // 6
println(word[0]) // K
println(word.uppercase()) // KOTLIN
val table = buildString {
for (i in 1..3) {
append("3 x ").append(i).append(" = ").append(3 * i).append('\n')
}
}
print(table)
println("42".toInt() + 1) // 43
println("abc".toIntOrNull()) // null
println("3.5".toDoubleOrNull()) // 3.5
}လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ type ရွေးချယ်ပုံ
လက်တွေ့မှာ type ရွေးတာက ဒေတာက ဘယ်လောက်ကြီးနိုင်လဲ၊ ဘယ်လောက် တိကျရမလဲ မေးခွန်းနှစ်ခုကနေ စပါတယ်။ ကောင်တာနဲ့ index အတွက် `Int` ကို သုံးပါတယ်။ Timestamp (`System.currentTimeMillis()` က `Long` ပြန်ပေးပါတယ်)၊ database ID၊ file size နဲ့ စုစုပေါင်း ကြီးကြီးတွေအတွက်တော့ `Long` ကို သုံးပါတယ်၊ ဘာကြောင့်လဲဆိုတော့ `Int` က ၂.၁ billion ကျော်ရင် error မပေးဘဲ ပျက်သွားလို့ပါ။ တိုင်းတာတဲ့ တန်ဖိုး (အပူချိန်၊ အမြင့်) တွေအတွက် `Double` ပါ။
ငွေကြေးကိုတော့ `Double` နဲ့ မသိမ်းကြပါဘူး၊ ဒသမ ရှုပ်ထွေးမှုကြောင့် ပြဿနာ ပေါ်လို့ပါ။ အသေးဆုံး ယူနစ် (ပြား၊ cent) ကို `Long` နဲ့ သိမ်းတာ ဒါမှမဟုတ် `BigDecimal` သုံးတာ ပုံမှန်ပါ။ အပြင်က ဝင်လာတဲ့ စာသား (form၊ file၊ API) ကိုလည်း `toInt()` ထက် `toIntOrNull()` နဲ့ အရင် စစ်ပါတယ်၊ ဒါမှ မမျှော်လင့်တဲ့ input တစ်ခုကြောင့် program တစ်ခုလုံး မပျက်မှာပါ။ ဒါက နောက်ပိုင်း null safety lesson တွေရဲ့ အခြေခံ အကြောင်းရင်းလည်း ဖြစ်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Int ကို Long ဒါမှမဟုတ် Double ထဲ တိုက်ရိုက်ထည့်တာ
`val count: Int = 5` ရှိပြီး `val total: Long = count` လို့ ရေးရင် `error: initializer type mismatch: expected 'Long', actual 'Int'.` ပေါ်ပါတယ်၊ `val d: Double = count` ကလည်း အတူတူပါပဲ။ `count.toLong()` ဒါမှမဟုတ် `count.toDouble()` လို့ ကိုယ်တိုင် ပြောင်းပါ။ `count == 5L` လို့ နှိုင်းယှဉ်တာလည်း မရပါဘူး၊ `5` လို့ပဲ ရေးပါ။
2. Char ကို double quote နဲ့ ရေးတာ
`val grade: Char = "A"` က `error: initializer type mismatch: expected 'Char', actual 'String'.` ပေးပါတယ်၊ `"A"` က String တစ်ခုမို့ပါ။ Char အတွက် `'A'` လို single quote သုံးပါ။
3. Int ချင်း စားပြီး ဒသမ မျှော်တာ၊ overflow ကို မထင်ထားတာ
`(7 + 8) / 2` က 7.5 မဟုတ်ဘဲ 7 ပါ၊ ဒသမ လိုချင်ရင် တစ်ဖက်ကို `2.0` လို Double ပေးပါ။ ကြီးတဲ့ ဂဏန်းတွေ မြှောက်တဲ့အခါ `Int` နှစ်ခုကို မြှောက်ပြီးမှ `Long` ထဲ ထည့်ရင် အရင် wrap ဖြစ်ပြီးသားပါ — `toLong()` ကို မြှောက်မယ့် operand တစ်ခုခုမှာ အရင် ခေါ်ပါ။
4. Int ထက်ကြီးတဲ့ literal ကို Int လို့ ကြေညာတာ
`val big: Int = 3000000000` က `error: initializer type mismatch: expected 'Int', actual 'Long'.` ပေးပါတယ်၊ ဂဏန်းက `Int` ထဲ မဆန့်လို့ပါ။ `val big = 3_000_000_000` လို့ type မရေးဘဲ ထားရင် Kotlin က `Long` လို့ ယူပေးပါတယ်၊ ဒါမှမဟုတ် `val big: Long = 3_000_000_000` လို့ ရေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- ကိန်းပြည့်ကို `Int`၊ ဒသမကို `Double` နဲ့ စပါ၊ ကြီးနိုင်ခြေရှိမှ `Long` ကို ပြောင်းပါ။
- Conversion ကို ဂဏန်းတွက်မယ့် operand မှာ အရင် လုပ်ပါ — `a.toLong() * b`၊ `(a * b).toLong()` မဟုတ်ပါ။
- ဂဏန်းရှည်တွေကို `1_000_000` လို underscore နဲ့ ရေးပြီး `Long` literal မှာ `L` ကို အကြီးနဲ့ ရေးပါ။
- ငွေကြေးကို `Double` နဲ့ မသိမ်းပါနဲ့၊ အသေးဆုံး ယူနစ်ကို `Long` နဲ့ သိမ်းပါ။
- User input ကို `toInt()` အစား `toIntOrNull()` နဲ့ ပြောင်းပါ။
Mini challenge
Mini challenge: download size report
Download file တစ်ခုရဲ့ အရွယ်အစားကို megabyte နဲ့ ပေးထားပါတယ်။ Byte အရေအတွက်ကို မှန်မှန်ကန်ကန် တွက်ပြီး Int နဲ့ တွက်ရင် ဘာမှားလဲဆိုတာကိုပါ ယှဉ်ပြပါ။
- `val megabytes = 3_000` (Int) ကို ကြေညာပါ
- `wrongBytes` ကို Int ချည်းနဲ့ `megabytes * 1024 * 1024` လို့ တွက်ပြီး `bytes` ကို `toLong()` အရင်လုပ်ကာ တွက်ပါ
- `gigabytes` ကို `bytes` ကို 1024³ နဲ့ စားပြီး `Double` အဖြစ် ရအောင် တွက်ပါ
- `fitsInInt` ဆိုတဲ့ Boolean နဲ့ `bytes` က `Int.MAX_VALUE` ထဲ ဆန့်မဆန့် စစ်ပြီး ရလဒ်လေးခုလုံးကို line တစ်ကြောင်းစီ print ထုတ်ပါ
fun main() {
val megabytes = 3_000
// TODO: wrongBytes: plain Int arithmetic (megabytes * 1024 * 1024)
// TODO: bytes: the same maths, but widen to Long first
// TODO: gigabytes: bytes divided by 1024 cubed, as a Double
// TODO: fitsInInt: a Boolean, does bytes fit within Int.MAX_VALUE?
// TODO: print the four results, one per line
}ရလဒ်: ထွက်ရမယ့် output: Int arithmetic: -1149239296 Long arithmetic: 3145728000 Gigabytes: 2.9296875 Fits in an Int: false
အရိပ်အမြွက် ကြည့်ရန်
`megabytes.toLong() * 1024 * 1024` မှာ ပထမ operand က `Long` ဖြစ်လို့ ကျန်တာကို Long နဲ့ ဆက်တွက်ပါတယ်။ Double ရဖို့ `bytes / (1024.0 * 1024 * 1024)` လို့ ရေးပါ — `1024.0` က Double ဖြစ်လို့ ကိန်းပြည့်စားခြင်း မဖြစ်တော့ပါဘူး။ `bytes <= Int.MAX_VALUE` ကို Long နဲ့ Int နှိုင်းယှဉ်လို့ရပါတယ်။
အဖြေ ကြည့်ရန်
fun main() {
val megabytes = 3_000
val wrongBytes = megabytes * 1024 * 1024
val bytes = megabytes.toLong() * 1024 * 1024
val gigabytes = bytes / (1024.0 * 1024 * 1024)
val fitsInInt = bytes <= Int.MAX_VALUE
println("Int arithmetic: $wrongBytes")
println("Long arithmetic: $bytes")
println("Gigabytes: $gigabytes")
println("Fits in an Int: $fitsInInt")
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- `Int` နဲ့ `Double` က default ဖြစ်ပြီး `Long` ကို `5L` ဒါမှမဟုတ် `toLong()` နဲ့ ရပါတယ်၊ Char က single quote၊ String က double quote ပါ။
- Kotlin မှာ implicit widening မရှိပါဘူး — `toLong()`, `toInt()`, `toDouble()` ကို ကိုယ်တိုင် ခေါ်ရပါတယ်။
- Integer overflow က error မပေးဘဲ wrap ဖြစ်ပြီး `Int / Int` က ဒသမကို ဖြတ်ပစ်ပါတယ်၊ `toInt()` လည်း round မလုပ်ပါဘူး။
- `buildString` နဲ့ string template တွေက စာသား တည်ဆောက်ဖို့ ကောင်းပြီး `toIntOrNull()` က input ကို ဘေးကင်းစွာ parse လုပ်ပေးပါတယ်။
Kotlin language guide — Kotlin