ရိုးရိုးလေး ရှင်းပြမယ်
Variable ကို ရိုးရိုးလေး ပြောရရင်
Variable ဆိုတာ data တစ်ခုကို သိမ်းထားဖို့ နာမည်တပ်ထားတဲ့ သေတ္တာလေးတစ်ခုပါ။ `age` ဆိုတဲ့ သေတ္တာထဲမှာ `30` ကို ထည့်ထားပြီး နောက်မှ `31` နဲ့ အစားထိုးလို့ ရပါတယ်။
Java မှာ ထူးခြားတာက သေတ္တာတိုင်းမှာ ဘာထည့်လို့ရလဲဆိုတဲ့ label (type) ကပ်ထားရတာပါ။ Java က 'ဒီသေတ္တာထဲ ဘာအမျိုးအစား ထည့်မှာလဲ' လို့ အစကတည်းက မေးပါတယ်။ `int` label ကပ်ထားတဲ့ သေတ္တာထဲကို စာသား ထည့်ဖို့ ကြိုးစားရင် program မ run ခင်ကတည်းက compiler က တားပါတယ်။
Problem → Why → Solution
ပြဿနာ: သေတ္တာထဲ မှားတာထည့်တာ၊ ဗလာသေတ္တာကို ဖွင့်တာ
Form တစ်ခုကနေ အသက်ကို ဖတ်ပြီး variable ထဲ ထည့်တဲ့ code ကို ရေးနေတယ် ဆိုပါစို့။ Form က အမြဲ စာသားပဲ ပေးလို့ `"30"` ကို `int` ထဲ တိုက်ရိုက် ထည့်မိပါတယ်။ ပြီးတော့ `total` ကို ကြေညာထားပေမဲ့ value မထည့်ရသေးဘဲ print ထုတ်ဖို့ ကြိုးစားမိပါတယ်။
တချို့ language တွေမှာ ဒီလို code က run ပြီး `undefined` ဒါမှမဟုတ် မှားတဲ့ value တစ်ခုကို တိတ်တိတ်လေး ထုတ်ပေးပါတယ်။ Java ကတော့ နှစ်ခုလုံးကို compile time မှာ ငြင်းပါတယ်။
public class Main {
public static void main(String[] args) {
int age = "30"; // error: incompatible types:
// String cannot be converted to int
int total;
System.out.println(total); // error: variable total might not
// have been initialized
}
}ဘာကြောင့် Java က တင်းကျပ်လဲ
Variable တစ်ခုရဲ့ type ကို ကြေညာပြီးတာနဲ့ program တစ်ခုလုံးမှာ မပြောင်းတော့ပါဘူး (static typing)။ ဒါကြောင့် compiler က variable တိုင်းကို ဘာ value မျိုး ထည့်ခွင့်ရှိလဲ အတိအကျ သိပြီး မကိုက်တာကို ကြိုဖမ်းနိုင်ပါတယ်။ Value မထည့်ရသေးတဲ့ local variable ကို ဖတ်ခွင့်မပေးတာကလည်း 'အမှိုက် value' ကြောင့်ဖြစ်တဲ့ bug တစ်မျိုးလုံးကို ဖယ်ရှားပေးပါတယ်။
အဓိက သဘောတရား: type, value, scope
Variable တစ်ခုနဲ့ ပတ်သက်ပြီး လုပ်ရပ်သုံးမျိုး ရှိပါတယ်။ Declaration (`int age;`) က type နဲ့ နာမည်ကို ကြေညာတာပါ။ Initialization (`int age = 30;`) က ကြေညာတဲ့အချိန်မှာပဲ ပထမ value ကို ထည့်တာပါ။ Assignment (`age = 31;`) က ရှိပြီးသား variable ထဲကို value အသစ် ထည့်ပြီး အဟောင်းကို အစားထိုးတာပါ။ Assignment မှာ type ကို ထပ်မရေးရပါဘူး၊ ထပ်ရေးရင် variable အသစ် ကြေညာသလို ဖြစ်ပြီး 'already defined' error ရပါတယ်။
Method ထဲမှာ ကြေညာတဲ့ local variable တွေမှာ default value မရှိပါဘူး။ Compiler က code လမ်းကြောင်းတိုင်းကို စစ်ပြီး variable ကို ဖတ်တဲ့နေရာ မရောက်ခင် value သေချာပေါက် ထည့်ပြီးမှ (definitely assigned ဖြစ်မှ) လက်ခံပါတယ်။
Variable တိုင်းမှာ scope ရှိပါတယ်: သူ့ကို ကြေညာထားတဲ့ `{ }` block ရဲ့ အထဲမှာပဲ မြင်ရပြီး block ပိတ်သွားတာနဲ့ နာမည်က ပျောက်သွားပါတယ်။ Block ထဲမှာ ကြေညာတဲ့ variable ကို block အပြင်ကနေ သုံးရင် 'cannot find symbol' ဖြစ်ပါတယ်။
| Keyword | ဘာလုပ်လဲ | သတိထားရန် |
|---|---|---|
| `final` | တစ်ခါ assign ပြီးရင် ထပ်မပြောင်းနိုင်ဘူး | ပြန် assign ရင် compile error |
| `var` | Initializer ကနေ type ကို compiler က ခန့်မှန်းတယ် | Type က ပုံသေ၊ dynamic မဟုတ်ဘူး |
`var` ကို Java 10 မှာ ထည့်ခဲ့ပြီး local variable တွေအတွက်ပဲ သုံးလို့ရပါတယ်။ `var city = "Yangon";` ဆိုရင် compiler က `city` ကို `String` လို့ ဆုံးဖြတ်ပြီး အဲဒီနောက် ဂဏန်းထည့်ဖို့ ကြိုးစားရင် ငြင်းပါတယ်။ Initializer မပါရင် ခန့်မှန်းစရာ မရှိလို့ `var x;` က compile error ပါ။ Variable နာမည်တွေကို `totalScore` လို camelCase နဲ့ ရေးပြီး စာလုံး၊ digit, `_`, `$` ပဲ သုံးရပါတယ်၊ digit နဲ့ မစရပါဘူး၊ `class` လို keyword လည်း မဖြစ်ရပါဘူး။
- Type နဲ့ နာမည်ကို ကြေညာတယ်
- ပထမ value ကို ထည့်တယ်
- လိုသလောက် ပြန် assign လုပ်တယ်
- Block ပိတ်ရင် နာမည် ပျောက်တယ်
`final` က တတိယအဆင့်ကို ပိတ်ပြီး `var` က ပထမအဆင့်မှာ type ကို compiler ကို ရွေးခိုင်းတာပါ၊ type ကတော့ အမြဲ ပုံသေပါ။
Variable ပုံစံများ
Type name = value; // declare + initialize
name = newValue; // reassign (same type, no type keyword)
Type later; // declare now...
later = value; // ...assign before the first read
final Type fixed = value; // can never be reassigned
var inferred = value; // type inferred at compile time (locals only)Variable ကို ကြေညာတဲ့ line တစ်ခုတည်းမှာပဲ type ကို ရေးပါတယ်။ ပြန် assign တဲ့အခါ နာမည်နဲ့ value ပဲ လိုပါတယ်။ `final` နဲ့ `var` က ကြေညာချက်ရှေ့မှာ ထည့်ရတဲ့ ရွေးချယ်စရာ keyword တွေပါ။
Profile တစ်ခုကို variable တွေနဲ့ သိမ်းခြင်း
အောက်က program က လူတစ်ယောက်ရဲ့ profile ကို type မတူတဲ့ variable လေးခုထဲ သိမ်းပြီး `age` ကို ပြန် assign လုပ်၊ `final` နဲ့ `var` ကိုလည်း သုံးပြပါတယ်။ Run မလုပ်ခင် `Age:` line မှာ 30 လား 31 လား ထွက်မလဲ ကြိုမှန်းကြည့်ပါ။ ပြီးရင် `maxAge = 130;` ဆိုတဲ့ line တစ်ကြောင်း ထပ်ထည့်ပြီး compiler ဘာပြောလဲ ကိုယ်တိုင် စမ်းကြည့်ပါ။
public class Main {
public static void main(String[] args) {
String name = "Aung";
int age = 30;
double height = 5.8;
boolean isStudent = false;
age = age + 1; // reassign: the old value is replaced
final int maxAge = 120; // final: can never be reassigned
var city = "Yangon"; // var: the compiler infers String
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Student: " + isStudent);
System.out.println("City: " + city + ", max age " + maxAge);
}
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Declare and initialize
int age = 30;`int` type ရှိတဲ့ `age` ကို ကြေညာပြီး `30` ကို ချက်ချင်း ထည့်ပါတယ်။ ဒီနောက် `age` ထဲမှာ ကိန်းပြည့်ပဲ ရှိနိုင်ပါတယ်။
- Reassign using the old value
age = age + 1; // reassign: the old value is replacedညာဘက်ကို အရင်တွက်ပါတယ်: `age` ရဲ့ လက်ရှိ value `30` ကို ဖတ်ပြီး `1` ပေါင်းလို့ `31` ရပါတယ်။ ပြီးမှ အဲဒီ `31` ကို `age` ထဲ ပြန်ထည့်ပါတယ်။ Type ကို ထပ်မရေးတာ သတိပြုပါ။
- A constant
final int maxAge = 120; // final: can never be reassigned`final` ကြောင့် `maxAge = 130;` လို့ နောက်မှ ရေးရင် 'cannot assign a value to final variable maxAge' ဆိုပြီး compile error ဖြစ်ပါတယ်။
- Type inference with var
var city = "Yangon"; // var: the compiler infers String`"Yangon"` က String ဖြစ်လို့ `city` ရဲ့ type က `String` ဖြစ်သွားပါတယ်။ `String city = "Yangon";` လို့ ရေးတာနဲ့ အတူတူပါပဲ၊ run time မှာ type ပြောင်းလို့ မရပါဘူး။
- Joining values into text
System.out.println("City: " + city + ", max age " + maxAge);String နဲ့ `+` ကို တွဲသုံးရင် ကျန်တဲ့ value တွေကို စာသားအဖြစ် ပြောင်းပြီး ဆက်ပါတယ်၊ ဒါကြောင့် `120` ကလည်း စာသားအဖြစ် ပါလာပါတယ်။
Name: Aung
Age: 31
Height: 5.8
Student: false
City: Yangon, max age 120Scope: variable တစ်ခု ဘယ်နေရာထိ မြင်ရလဲ
Block တစ်ခုထဲမှာ ကြေညာတဲ့ variable က အဲဒီ block နဲ့ သူ့အထဲက block တွေမှာပဲ မြင်ရပါတယ်။ အပြင်ဘက် block က variable တွေကိုတော့ အထဲကနေ သုံးလို့ရပါတယ်။ ဒါကြောင့် variable တစ်ခုကို သုံးမယ့်နေရာ အားလုံးကို ဖုံးနိုင်တဲ့ အသေးဆုံး block ထဲမှာ ကြေညာပါ။
Nested scopes
-------------
main() { <- total lives here
int total = 0;
if (...) { <- bonus lives only in here
int bonus = 5;
total = total + bonus; OK: total is visible inside
}
print(bonus); <- ERROR: cannot find symbol bonus
}လက်တွေ့ project တွေမှာ
လက်တွေ့ app တွေမှာ
User profile, ပစ္စည်းဈေးနှုန်း, login ဝင်ထားမထား, game score, ကုန်ပစ္စည်း လက်ကျန် စတဲ့ app data အားလုံးက ဒီ variable သဘောတရားကနေ စပါတယ်။ Production code မှာ developer တွေက variable နာမည်ကို ရှည်ပေမဲ့ အဓိပ္ပာယ်ရှင်းအောင် ပေးပါတယ်: `x` အစား `remainingStock`, `flag` အစား `isEmailVerified` လိုမျိုးပါ။ ခြောက်လကြာပြီး code ပြန်ဖတ်တဲ့သူ (အများအားဖြင့် ကိုယ်တိုင်) နာမည်ကနေ အဓိပ္ပာယ်ကို ချက်ချင်း နားလည်ရပါမယ်။
Team အများစုက 'မပြောင်းရင် `final` ထည့်' ဆိုတဲ့ စည်းမျဉ်းကို လိုက်ပါတယ်။ Method ရှည်ရှည်ထဲမှာ value တစ်ခု မပြောင်းဘူးဆိုတာ သေချာသိရင် code ဖတ်ရတာ ပိုလွယ်ပြီး မတော်တဆ ပြန် assign မိတာကိုလည်း compiler က ဖမ်းပေးပါတယ်။ `var` ကိုတော့ `var orders = new ArrayList<Order>();` လို type က ညာဘက်မှာ ထင်ရှားနေတဲ့ နေရာမှာ သုံးပြီး `var x = getData();` လို type မသိနိုင်တဲ့ နေရာမှာ ရှောင်ကြပါတယ်။
အမှားများ
Variable နဲ့ ပတ်သက်ပြီး မကြာခဏ ဖြစ်တတ်တဲ့ အမှားများ
ပြန် assign တဲ့အခါ type ကို ထပ်ရေးတာ
`int score = 5;` ပြီးနောက် `int score = 6;` လို့ ရေးရင် Java က variable အသစ် ထပ်ကြေညာတယ်လို့ ယူပြီး 'variable score is already defined' ဆိုပြီး ငြင်းပါတယ်။ ရှိပြီးသား variable ကို ပြောင်းချင်ရင် `score = 6;` လို့ပဲ ရေးပါ။
`var` ကို dynamic type လို့ ထင်တာ
JavaScript ကနေ လာတဲ့သူတွေ `var count = 10;` ပြီးနောက် `count = "ten";` လို့ ရေးကြည့်တတ်ပါတယ်။ `count` က compile time မှာ `int` ဖြစ်သွားပြီးပြီမို့ 'incompatible types: String cannot be converted to int' ရပါတယ်။ `var` က စာရိုက်ရတာ သက်သာအောင်ပဲ ကူညီတာပါ၊ type စည်းမျဉ်းကို မဖြေလျှော့ပါဘူး။
Block ထဲက variable ကို အပြင်က သုံးတာ
`if` ထဲမှာ ကြေညာတဲ့ variable ကို `if` ပြီးမှ print ထုတ်ချင်ရင် 'cannot find symbol' ရပါတယ်။ Variable ကို block မတိုင်ခင် ကြေညာပြီး block ထဲမှာ assign လုပ်ပါ။
Best Practices
Variable နာမည်ကောင်း ပေးနည်း
Compiler က စည်းမျဉ်းနဲ့ ကိုက်တဲ့ နာမည်မှန်သမျှ လက်ခံပေမဲ့ လူဖတ်လို့ကောင်းတဲ့ နာမည်ကတော့ သီးခြားကိစ္စပါ။ Variable တစ်ခုက ဘာကို ကိုယ်စားပြုလဲဆိုတာ နာမည်ကနေ ပေါ်လွင်ရပါမယ်။ Boolean variable တွေကို `isActive`, `hasPaid` လို မေးခွန်းပုံစံ နာမည်ပေးရင် `if` ထဲမှာ စာကြောင်းတစ်ကြောင်းလို ဖတ်လို့ရပါတယ်။ Variable ကို သုံးမယ့်နေရာနဲ့ နီးနီးမှာ ကြေညာပြီး တစ်ခုထက်ပိုတဲ့ အလုပ်အတွက် variable တစ်ခုတည်းကို ပြန်မသုံးပါနဲ့။
| နာမည် | ရလား | အကြောင်းရင်း |
|---|---|---|
| `totalPrice` | ရတယ်၊ အကောင်းဆုံး | camelCase နဲ့ အဓိပ္ပာယ်ရှင်းတယ် |
| `2ndPlace` | မရဘူး | Digit နဲ့ စလို့ မရဘူး |
| `class` | မရဘူး | Keyword ဖြစ်နေတယ် |
| `tp` | ရတယ်၊ ဒါပေမဲ့ မကောင်းဘူး | ဘာကို ဆိုလိုလဲ မသိရဘူး |
Mini challenge
Mini challenge: score board နဲ့ swap
ကစားသမား နှစ်ယောက်ရဲ့ နာမည်နဲ့ score ကို variable တွေထဲ သိမ်းပါ။ ပထမကစားသမားကို `final` bonus ပေါင်းပေးပြီးနောက် score များတဲ့သူ အပေါ်ရောက်အောင် temporary variable သုံးပြီး နာမည်နဲ့ score ကို swap လုပ်ပါ။
- `final int bonus = 30;` ကို သုံးပါ
- Mya က 90 နဲ့ စပြီး bonus ရပြီးနောက် 120 ဖြစ်ရပါမယ်၊ Ko Ko က 140 ပါ
- Swap အတွက် temporary variable နှစ်ခု (နာမည်အတွက်တစ်ခု၊ score အတွက်တစ်ခု) ကို သုံးပါ
- `1st: Ko Ko (140)` နဲ့ `2nd: Mya (120)` ကို print ထုတ်ပါ
public class ScoreBoard {
public static void main(String[] args) {
// TODO: a final int bonus of 30
// TODO: String first = "Mya", String second = "Ko Ko"
// TODO: int firstScore = 90, int secondScore = 140
// TODO: add the bonus to firstScore (reassign, do not redeclare)
// TODO: swap first/second and firstScore/secondScore with temp variables
// TODO: print "1st: <name> (<score>)" and "2nd: <name> (<score>)"
}
}ရလဒ်: `1st: Ko Ko (140)` နဲ့ `2nd: Mya (120)` ဆိုပြီး line နှစ်ကြောင်း ထွက်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
Swap လုပ်တဲ့အခါ `String tempName = first; first = second; second = tempName;` လို အဆင့်သုံးဆင့် လိုပါတယ်။ Temp မပါရင် value တစ်ခု ပျောက်သွားပါမယ်။
အဖြေ ကြည့်ရန်
public class ScoreBoard {
public static void main(String[] args) {
final int bonus = 30;
String first = "Mya";
String second = "Ko Ko";
int firstScore = 90;
int secondScore = 140;
firstScore = firstScore + bonus;
String tempName = first;
first = second;
second = tempName;
int tempScore = firstScore;
firstScore = secondScore;
secondScore = tempScore;
System.out.println("1st: " + first + " (" + firstScore + ")");
System.out.println("2nd: " + second + " (" + secondScore + ")");
}
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Variable ကို `Type name = value;` နဲ့ ကြေညာပြီး `name = newValue;` နဲ့ ပြန် assign လုပ်ပါ
- Local variable ကို value ထည့်ပြီးမှ ဖတ်လို့ရပါတယ် (definite assignment)
- `final` က ပြန် assign ကို တားပြီး `var` က compile time မှာ type ကို ခန့်မှန်းပါတယ်
- Variable တစ်ခုက သူ့ကို ကြေညာတဲ့ `{ }` block ထဲမှာပဲ ရှိပါတယ်