Java ပရိုဂရမ်မင်း ဘာသာရပ်တွင်၊ အစားထိုးဖော်ပြချက်များကို သတ်မှတ် ရေးသားရာတွင် Assignment Operators ကို၎င်း၊ ကိန်းဂဏာန်းများကို တွက်ချက်ရာတွင် Arithmetic Operators ကို၎င်း၊ ပတ်သက်ပုံများကို ဖော်ပြရာတွင် Relational Operators ကို၎င်း၊ လော့ဂျစ်များကို ရေးသားရာတွင် Logical Operators တို့ကိုအသုံးပြုပါသည်။
Assignment Operators များ
ဤ Operator သည် ရင်းနှီးပြီးဖြစ်ပါလိမ့်မည်။ ကိန်းတစ်ခုအား သတ်မှတ်ရာတွင် အသုံးပြုလေ့ရှိပြီး a = b ဆိုပါက၊ a ၏ တန်ဖိုးအား b ဖြင့် အစားထိုးသည်ဟု အဓိပ္ပါယ်ရပါသည်။
လက်တွေ့ အောက်ပါ နမှုနာကို လေ့လာကြည့်ပါမည်။
public class Sapmle {
public static void main(String[] args) {
int i = 10;
i = i + 20;
System.out.println("i : " + i);
}
}
တဖန် ကိန်းတစ်ခုအား အပေါင်းအနှုတ်ပြုလုပ်၍ ထိုကိန်းအား အစားပြန်ထိုးသည်ကို y += 1 ဟု ရေးသားနိုင်ပါသည်။public class Sapmle {
public static void main(String[] args) {
int i = 10;
i += 20;
System.out.println("i : " + i);
}
}
နှစ်မျိုးလုံး၏ အဖြေမှာ အောက်ပါအတိုင်း ဖြစ်၏။
i : 30
Arithmetic Operators များ
Java ပရိုဂရမ်မင်း ဘာသာရပ်တွင် အခြားသော ဘာသာရပ်များကဲ့သို့ပင် အပေါင်း (+) ၊ အနှုတ် (-) ၊ အမြှောက် (*) ၊ အစား (/) နှင့် အကြွင်းရှာ (%) Operators တို့ကို အသုံးပြုပါသည်။ အောက်ပါ နမှုနာထဲတွင် int ကိန်းနှစ်လုံး၊ double ကိန်းနှစ်လုံးကို အသုံးပြု၍ Arithmetic Operators များ၏ အသုံးပြုပုံကို လေ့လာ သွားပါဦးမည်။
ArithmeticProg.java
public class ArithmeticProg {
public static void main(String[] args) {
// ကိန်းများ
int i = 10;
int j = 20;
double x = 10.5;
double y = 20.5;
// အပေါင်းသင်္ကေတ အားအသုံးပြုခြင်း
System.out.println("Adding");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));
// အနှုတ်သင်္ကေတ အားအသုံးပြုခြင်း
System.out.println("Subtracting");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));
// အမြှောက်သင်္ကေတ အားအသုံးပြုခြင်း
System.out.println("Multiplying");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));
// အစားသင်္ကေတ အားအသုံးပြုခြင်း
System.out.println("Dividing");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));
// အကြွင်းရှာသင်္ကေတ အားအသုံးပြုခြင်း
System.out.println("Modulus");
System.out.println(" i % j = " + (i % j));
System.out.println(" x % y = " + (x % y));
}
}
တဖန် ပရိုဂရမ်များတွင် ပေါင်းခြင်း၊ နှုတ်ခြင်းများကို ++, -- Operators တို့ကို အသုံးပြု၍လည်း ရေးသားနိုင်ပါသည်။ အဆိုပါသင်္ကေတများအား ကိန်း၏ အရှေ့တွင် ထားခြင်းနှင့် အနောက်တွင်ထားခြင်းသည် Java ပရိုဂရမ်မင်းတွင် အဓိပ္ပါယ်ကွာခြားပါသည်။ အောက်ပါ နမှုနာကို ကြည့်ပါ။
public class Sapmle {
public static void main(String[] args) {
int i = 1;
System.out.println("i++ : " + i++);
System.out.println("i : " + i);
System.out.println("++i : " + ++i);
System.out.println("i : " + i);
}
}
အဖြေမှာ အောက်ပါအတိုင်း ဖြစ်၏။
i++ : 1 i : 2 ++i : 3 i : 3အပေါ်က ရလဒ်နှစ်ခုကို ကြည့်ခြင်းအားဖြင့် i++ ၏ ရလဒ်မှာ ၁ ဖြစ်သော်လည်း၊ i ၏ တန်ဖိုးကိုမူ ၁တိုးသွားပြီး ၂ဖြစ်လာသည်ကို တွေ့နိုင်၏။ အောက်ဆုံးစာကြောင်း နှစ်ကြောင်းကို ကြည့်ခြင်းအားဖြင့်၊ ++i ၏ ရလဒ်ကိုကော i ၏ တန်ဖိုးကိုပါ တစ်တိုးပြီး နှစ်မျိုးစလုံး ၃ ရရှိသည်ကို တွေ့နိုင်ပါသည်။ အနှုတ်သင်္ကေတတွင်လည်း အလားတူပင်ဖြစ်၏။
Relational Operators များ
Relational Operators များသည် ကိန်းနှစ်ခု၏ ပတ်သက်ပုံကို ဖော်ပြပေးနိုင်ပါသည်။ အတိအကျ ဆိုရမည် ဆိုလျှင် Relational Operators တို့ဖြင့် ကိန်းနှစ်ခု၏ တန်ဖိုးသည် တူညီခြင်း၊ မတူညီခြင်း၊ ကြီးခြင်း၊ ငယ်ခြင်း အစရှိသည် တို့ကို စမ်းသပ်နိုင်ပါသည်။
အသုံးပြုနိုင်သော Operator များမှာ အောက်ပါအတိုင်းဖြစ်၏။
| အသုံးပြုပုံ | ရှင်းလင်းချက် |
| op1 > op2 | op1 သည် op2 အထက် တန်ဖိုးဖြစ်၏။ |
| op1 >= op2 | op1 သည် op2 နှင့် အထက် တန်ဖိုးဖြစ်၏။ |
| op1 < op2 | op1 သည် op2 အောက် တန်ဖိုးဖြစ်၏။ |
| op1 <= op2 | op1 သည် op2 နှင့် အောက် တန်ဖိုးဖြစ်၏။ |
| op1 == op2 | op1 သည် op2 နှင့် တူညီသည် |
| op1 != op2 | op1 သည် op2 နှင့် မတူညီပါ |
လက်တွေ့ စမ်းသပ်ရေးသားကြည့်ပါဦးမည်။
public class RelationalProg {
public static void main(String[] args) {
//a few numbers
int i = 37;
int j = 42;
int k = 42;
//greater than
System.out.println("Greater than...");
System.out.println(" i > j = " + (i > j)); //false
System.out.println(" j > i = " + (j > i)); //true
System.out.println(" k > j = " + (k > j)); //false
//(they are equal)
//greater than or equal to
System.out.println("Greater than or equal to...");
System.out.println(" i >= j = " + (i >= j)); //false
System.out.println(" j >= i = " + (j >= i)); //true
System.out.println(" k >= j = " + (k >= j)); //true
//less than
System.out.println("Less than...");
System.out.println(" i < j = " + (i < j)); //true
System.out.println(" j < i = " + (j < i)); //false
System.out.println(" k < j = " + (k < j)); //false
//less than or equal to
System.out.println("Less than or equal to...");
System.out.println(" i <= j = " + (i <= j)); //true
System.out.println(" j <= i = " + (j <= i)); //false
System.out.println(" k <= j = " + (k <= j)); //true
//equal to
System.out.println("Equal to...");
System.out.println(" i == j = " + (i == j)); //false
System.out.println(" k == j = " + (k == j)); //true
//not equal to
System.out.println("Not equal to...");
System.out.println(" i != j = " + (i != j)); //true
System.out.println(" k != j = " + (k != j)); //false
}
}
အဖြေမှာ အောက်ပါအတိုင်း ဖြစ်၏။
Greater than... i > j = false j > i = true k > j = false Greater than or equal to... i >= j = false j >= i = true k >= j = true Less than... i < j = true j < i = false k < j = false Less than or equal to... i <= j = true j <= i = false k <= j = true Equal to... i == j = false k == j = true Not equal to... i != j = true k != j = false
Logical Operators များ
Java တွင် boolean လော့ဂျစ်များကို တွက်ချက် ဖော်ပြရာတွင် အသုံးပြုနိုင်သော Operator တို့ကို Logical Operators များဟု ခေါ်ပါသည်။ အသုံးများသော Operator များမှာ |, &, ^, !, ||, &&, ==, != တို့ ဖြစ်ကြ၏။
| သည် လော့ဂျစ် OR ၏ သင်္ကေတဖြစ်ပြီး၊ & သည် AND၊ ^ သည် XOR ဖြစ်ပြီး ! သည် NOT ဖြစ်၏။ || သည် short-circuit OR သင်္ကေတဖြစ်ပြီး && သည် short-circuit AND သင်္ကေတ ဖြစ်ပါသည်။ လက်တွေ့ ရေးသားကြည့်ပါမည်။
public class Sapmle {
public static void main(String args[]) {
// these are boolean variables
boolean A = true;
boolean B = false;
System.out.println("A|B = " + (A | B));
System.out.println("A&B = " + (A & B));
System.out.println("!A = " + (!A));
System.out.println("A^B = " + (A ^ B));
System.out.println("(A|B)&A = " + ((A | B) & A));
}
}
အဖြေမှာ အောက်ပါအတိုင်း ဖြစ်၏။A|B = true A&B = false !A = false A^B = true (A|B)&A = true
နောက်ရက်များတွင် Controls Flow Statements အကြောင်းကို ဖော်ပြပြီး၊ ဆက်လက်၍ Method နှင့် Class များ အကြောင်းကို ဖော်ပြသွားပါဦးမည်။
လေးစားစွာဖြင့်
မင်းလွင်
No comments:
Post a Comment