Thuta Learning
Java
BasicProgrammingအခြေခံ

Operators

Javaသင်ခန်းစာ 725 မိနစ်

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

  • int နဲ့ int စားရင် ဒသမ ပျောက်သွားတာနဲ့ % (remainder) ရလဒ်ကို run မလုပ်ခင် ခန့်မှန်းနိုင်ဖို့
  • int overflow ဖြစ်ရင် ဘာဖြစ်လဲ ရှင်းပြနိုင်ပြီး long ဒါမှမဟုတ် Math.addExact() နဲ့ ကာကွယ်နိုင်ဖို့
  • && နဲ့ || ရဲ့ short-circuit သဘောကို သုံးပြီး null ကို ဘေးကင်းကင်း စစ်နိုင်ဖို့
  • Precedence နဲ့ string concatenation ကြောင့် ဖြစ်တဲ့ မမျှော်လင့်တဲ့ ရလဒ်တွေကို ကွင်းခတ်ပြီး ပြင်နိုင်ဖို့

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

ရိုးရိုးလေး ရှင်းရရင်

Operator ဆိုတာ value တွေပေါ်မှာ အလုပ်တစ်ခု လုပ်ပေးတဲ့ သင်္ကေတလေးတွေပါ။ + က ပေါင်းပေးတယ်၊ < က နှိုင်းယှဉ်ပေးတယ်၊ && က condition နှစ်ခုကို ပေါင်းစစ်ပေးတယ်။ Calculator ပေါ်က ခလုတ်တွေလိုပါပဲ — ဂဏန်းတွေ ထည့်လိုက်၊ ခလုတ်နှိပ်လိုက်၊ ရလဒ်တစ်ခု ထွက်လာပါတယ်။

ထူးခြားချက်ကတော့ Java က ရလဒ်ရဲ့ type ကိုလည်း ဂရုစိုက်တာပါ။ 7 / 2 ကို calculator မှာ နှိပ်ရင် 3.5 ရပေမယ့် Java မှာ int နှစ်ခု စားလို့ 3 ပဲ ရပါတယ်။ ဒီ lesson မှာ ခလုတ်တစ်ခုချင်းက ဘာလုပ်လဲဆိုတာနဲ့ အဲဒီလို အံ့သြစရာ ရလဒ်တွေ ဘာကြောင့် ထွက်လာလဲဆိုတာ လေ့လာပါမယ်။

Problem → Why → Solution

ပြဿနာ: ပျမ်းမျှ အမှတ် မှားနေတယ်

ကျောင်းသားရဲ့ အမှတ် နှစ်ခုကို ပျမ်းမျှ တွက်ချင်လို့ အောက်က code ကို ရေးလိုက်တယ် ဆိုပါစို့။ 85 နဲ့ 90 ဆိုရင် 87.5 ရရမှာပါ။ ဒါပေမယ့် program က 87.0 ကို print ထုတ်ပါတယ်။ ဒုတိယ line ကလည်း "Total: 8590" ထုတ်ပြီး 175 မထုတ်ပါဘူး။ Compile error လည်း မရှိ၊ crash လည်း မဖြစ်လို့ bug ကို သတိမထားမိဘဲ report ထဲ ရောက်သွားနိုင်ပါတယ်။

Main.java
public class Main {
  public static void main(String[] args) {
    int first = 85;
    int second = 90;
    double average = (first + second) / 2;   // 87.0, not 87.5
    System.out.println("Average: " + average);
    System.out.println("Total: " + first + second); // "Total: 8590"
  }
}

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

(first + second) / 2 မှာ ဘက်နှစ်ဖက်လုံး int ဖြစ်လို့ Java က integer division လုပ်ပြီး 87 ရပါတယ်၊ double variable ထဲ ထည့်တာက တွက်ပြီးမှ ဖြစ်လို့ ပျောက်သွားတဲ့ .5 ပြန်မလာတော့ပါဘူး။ ဒုတိယ line မှာတော့ + ကို ဘယ်ကနေ ညာ အစဉ်လိုက် တွက်ပါတယ် — "Total: " + 85 က string ဖြစ်သွားပြီး အဲဒီ string ကို 90 ထပ်ဆက်လိုက်တာပါ။

Core concept: operator တိုင်းမှာ type နဲ့ အစဉ် ရှိတယ်

Arithmetic operator တွေက + - * / % ပါ။ ဘက်နှစ်ဖက်လုံး int ဖြစ်ရင် ရလဒ်လည်း int ပါ၊ ဒါကြောင့် / က ဒသမကို ဖြတ်ပစ်ပြီး zero ဘက်ကို truncate လုပ်ပါတယ် (7 / 2 က 3၊ -7 / 2 က -3)။ % က စားလို့ ကျန်တဲ့ အကြွင်းကို ပေးပြီး ရလဒ်ရဲ့ sign က ဘယ်ဘက် operand နဲ့ တူပါတယ် (-7 % 2 က -1)။ ဘက်တစ်ဖက်က double ဖြစ်ရင် Java က ကျန်တဲ့ဘက်ကို double အဖြစ် promote လုပ်ပြီးမှ တွက်လို့ 7 / 2.0 က 3.5 ရပါတယ်။ byte, short, char တွေကို တွက်ရင်လည်း အနည်းဆုံး int အဖြစ် အရင် promote လုပ်ပါတယ်။

int ရဲ့ range က -2,147,483,648 ကနေ 2,147,483,647 အထိပါ။ အဲဒီထက် ကျော်သွားရင် Java က error မပေးဘဲ ဟိုဘက်အစွန်ကို လှည့်ပတ် (wrap around) သွားပါတယ် — Integer.MAX_VALUE + 1 က -2147483648 ဖြစ်ပါတယ်။ int ကို သုညနဲ့ စားရင်တော့ ArithmeticException တက်ပေမယ့် double ကို သုညနဲ့ စားရင် Infinity ဒါမှမဟုတ် NaN ပဲ ပြန်ရပါတယ်။

Comparison operator တွေ (== != < <= > >=) က boolean ပြန်ပေးပါတယ်။ = က assign လုပ်တာ၊ == က နှိုင်းယှဉ်တာဆိုတာ သတိထားပါ။ Logical operator && (AND), || (OR), ! (NOT) တွေကတော့ boolean တွေကို ပေါင်းပါတယ်။

&& နဲ့ || က short-circuit လုပ်ပါတယ်။ a && b မှာ a က false ဆိုရင် အဖြေက false ဖြစ်ပြီးသားမို့ b ကို လုံးဝ မတွက်တော့ပါဘူး။ ဒါကြောင့် name != null && name.length() > 3 ဆိုတဲ့ guard က name null ဖြစ်နေရင်တောင် NullPointerException မတက်ပါဘူး။ & နဲ့ | ကို boolean ပေါ်မှာ သုံးရင်တော့ ဘက်နှစ်ဖက်လုံးကို အမြဲ တွက်ပါတယ်။

နောက်ဆုံးအနေနဲ့ precedence ရှိပါတယ်: * / % က + - ထက် အရင်၊ comparison က && ထက် အရင်၊ && က || ထက် အရင် တွက်ပါတယ်။ ++ နဲ့ -- မှာ prefix (++i) က တိုးပြီးမှ value ကို ပေးပြီး postfix (i++) က value အဟောင်းကို ပေးပြီးမှ တိုးပါတယ်။ += လို compound assignment တွေက ရလဒ်ကို variable ရဲ့ type ဆီ အလိုအလျောက် cast လုပ်ပေးပါတယ်။ သံသယရှိရင် ကွင်းခတ်ပါ — ကွင်းက code ဖတ်တဲ့သူအတွက်ပါ ရှင်းစေပါတယ်။

Expressionရလဒ်အကြောင်းရင်း
7 / 23int / int က int
7 / 2.03.52.0 က double မို့ promote
-7 % 2-1sign က ဘယ်ဘက် operand အတိုင်း
"A" + 1 + 2"A12"ဘယ်ကနေ ညာ၊ string ဖြစ်သွားပြီ
1 + 2 + "A""3A"ဂဏန်းနှစ်ခု အရင် ပေါင်း
Main.java
public class Main {
  public static void main(String[] args) {
    int i = 5;
    int a = i++;   // a gets 5, then i becomes 6
    int b = ++i;   // i becomes 7, then b gets 7
    System.out.println(a + " " + b + " " + i);

    byte small = 10;
    small += 5;    // OK: += casts back to byte (small = small + 5 would not compile)
    System.out.println(small);

    String name = null;
    boolean longName = name != null && name.length() > 3; // right side skipped
    System.out.println("Long name? " + longName);
  }
}
Expression တစ်ခုကို Java ဘယ်လို ဖတ်လဲ
  1. Precedence နဲ့ ကွင်းအတိုင်း အုပ်စုဖွဲ့
  2. Operand နှစ်ဖက်ရဲ့ type ကို ကြည့်
  3. လိုရင် int/double သို့ promote
  4. Type အတိုင်း တွက် (truncate/wrap)
  5. && / || က လိုမှ ညာဘက်ကို တွက်

ရလဒ်ကို ဘယ် variable ထဲ ထည့်မလဲဆိုတာက တွက်ပြီးမှ အရေးပါတာပါ — int / int ကို double ထဲ ထည့်လို့ ဒသမ ပြန်မလာပါဘူး။

အထွေထွေ ပုံစံ

java
type result = left OP right;          // + - * / %
boolean test = value COMPARE limit;     // == != < <= > >=
boolean both = conditionA && conditionB; // also ||, !
variable += amount;                      // also -= *= /= %=
double exact = intValue / (double) other;
long big = intValue * 1L * otherInt;     // widen before multiplying
type choice = condition ? valueIfTrue : valueIfFalse;

Operand နှစ်ခုကြားမှာ operator တစ်ခု ထားပြီး ရလဒ် type က operand တွေရဲ့ type ပေါ် မူတည်ပါတယ်။ ဒသမ လိုရင် တွက်ခင်မှာ (double) နဲ့ cast လုပ်ပြီး ကြီးတဲ့ ဂဏန်းတွေအတွက် 1L နဲ့ long ကို အရင် ပြောင်းပါ။ ? : က condition ပေါ်မူတည်ပြီး value နှစ်ခုထဲက တစ်ခုကို ရွေးပေးတဲ့ operator ပါ။

Example: ဈေးနှုန်းနဲ့ ဘူးထုပ်ပိုး တွက်ချက်ခြင်း

ပထမပိုင်းက လျှော့ဈေး တွက်ပြီး ဝယ်လို့ရမလား စစ်ပါတယ်။ ဒုတိယပိုင်းက မုန့် 7 ခုကို 3 ခုဝင် ဘူးတွေထဲ ထည့်တဲ့အခါ / နဲ့ % ကို ဘယ်လို တွဲသုံးလဲ ပြပြီး နောက်ဆုံး line က + ရဲ့ string concatenation ထောင်ချောက်ကို တမင် ပြထားပါတယ်။

Main.java
public class Main {
  public static void main(String[] args) {
    int price = 100;
    int discount = 15;
    int finalPrice = price - discount;

    boolean isAffordable = finalPrice <= 90;
    boolean hasStock = true;

    System.out.println("Final price: " + finalPrice);
    System.out.println("Can buy: " + (isAffordable && hasStock));

    int cookies = 7;
    int boxSize = 3;
    System.out.println("Full boxes: " + cookies / boxSize);
    System.out.println("Left over: " + cookies % boxSize);
    System.out.println("Exact share: " + cookies / (double) boxSize);
    System.out.println("Wrong grouping: " + cookies + boxSize);
  }
}

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

  1. Line 7boolean isAffordable = finalPrice <= 90;

    <= က comparison operator ဖြစ်လို့ ရလဒ်က true ဒါမှမဟုတ် false ပါ။ 85 <= 90 ဖြစ်လို့ true ကို boolean variable ထဲ သိမ်းထားပါတယ်။

  2. Line 11System.out.println("Can buy: " + (isAffordable && hasStock));

    && က ဘက်နှစ်ဖက်လုံး true မှ true ပါ။ ကွင်းခတ်ထားတာ အရေးကြီးပါတယ် — ကွင်းမပါရင် "Can buy: " + isAffordable က အရင် string ဖြစ်သွားပြီး string && boolean ဆိုတာ compile error ဖြစ်ပါတယ်။

  3. Lines 15-16System.out.println("Full boxes: " + cookies / boxSize);

    / က + ထက် precedence မြင့်လို့ 7 / 3 ကို အရင် တွက်ပါတယ်။ int စားခြင်းဖြစ်လို့ ဘူးပြည့် 2 ဘူး ရပြီး နောက် line က 7 % 3 နဲ့ ကျန်တဲ့ 1 ခုကို ပြပါတယ်။ / နဲ့ % ကို တွဲသုံးရင် "ဘယ်နှခု ပြည့်လဲ၊ ဘယ်နှခု ကျန်လဲ" ကို အတိအကျ ရပါတယ်။

  4. Line 17cookies / (double) boxSize

    (double) cast က boxSize ကို 3.0 အဖြစ် ပြောင်းလို့ cookies ကိုလည်း double အဖြစ် promote လုပ်ပြီး 2.3333333333333335 ရပါတယ်။ နောက်ဆုံး digit က double ရဲ့ binary representation ကြောင့်ပါ။

  5. Line 18System.out.println("Wrong grouping: " + cookies + boxSize);

    ကွင်းမပါလို့ ဘယ်ကနေ ညာ တွက်ပြီး "Wrong grouping: 7" ဖြစ်ပြီးမှ "3" ကို ဆက်လိုက်တာပါ — 10 မဟုတ်ဘဲ 73 ထွက်ပါတယ်။ (cookies + boxSize) လို့ ကွင်းခတ်ရင် 10 ရပါမယ်။

You should see
Final price: 85
Can buy: true
Full boxes: 2
Left over: 1
Exact share: 2.3333333333333335
Wrong grouping: 73

Overflow နဲ့ သုညနဲ့ စားခြင်း

Overflow က အန္တရာယ်များတာက အသံမထွက်လို့ပါ။ ပိုက်ဆံ total, millisecond timestamp, file size လို တန်ဖိုးကြီးတွေကို int နဲ့ တွက်ရင် ရလဒ်က အနုတ်ဂဏန်း ဖြစ်သွားနိုင်ပါတယ်။ ကာကွယ်နည်း နှစ်မျိုး ရှိပါတယ် — တွက်ခင်ကတည်းက long ကို သုံးတာ (max + 1L)၊ ဒါမှမဟုတ် overflow ဖြစ်ရင် exception ပစ်ပေးတဲ့ Math.addExact(), Math.multiplyExact() ကို သုံးတာပါ။ ပိုက်ဆံ တွက်တာဆိုရင် double ကို ရှောင်ပြီး long (ပြား/cent ယူနစ်) ဒါမှမဟုတ် BigDecimal ကို သုံးပါ။

Main.java
public class Main {
  public static void main(String[] args) {
    int max = Integer.MAX_VALUE;
    System.out.println(max + 1);              // wraps to the minimum
    long safe = max + 1L;                     // long arithmetic, no wrap
    System.out.println(safe);
    System.out.println(-7 / 2 + " " + -7 % 2); // truncates toward zero
    System.out.println(1.0 / 0);              // double: Infinity
    try {
      System.out.println(Math.addExact(max, 1));
    } catch (ArithmeticException e) {
      System.out.println("addExact: " + e.getMessage());
    }
    try {
      System.out.println(10 / 0);
    } catch (ArithmeticException e) {
      System.out.println("10 / 0: " + e.getMessage());
    }
  }
}

ဒီ code က -2147483648, 2147483648, -3 -1, Infinity, addExact: integer overflow နဲ့ 10 / 0: / by zero ကို အစဉ်လိုက် print ထုတ်ပါတယ်။ try / catch ကို exception lesson မှာ အသေးစိတ် လေ့လာပါမယ်၊ ဒီမှာတော့ program ဆက် run နိုင်အောင်ပဲ သုံးထားတာပါ။

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

Real project တွေမှာ ဘယ်လို သုံးလဲ

Pagination က / နဲ့ % ရဲ့ ဂန္ထဝင် ဥပမာပါ။ Item 95 ခုကို စာမျက်နှာတစ်ခု 10 ခုစီ ပြမယ်ဆိုရင် စာမျက်နှာ အရေအတွက်က (95 + 10 - 1) / 10 = 10 ဖြစ်ပြီး နောက်ဆုံး စာမျက်နှာမှာ 95 % 10 = 5 ခု ရှိပါတယ်။ % ကိုပဲ table row တွေကို အရောင်ခွဲပြတာ (i % 2 == 0)၊ ID တွေကို shard/bucket ခွဲတာ၊ စက္ကန့်တွေကို မိနစ်နဲ့ စက္ကန့်အဖြစ် ပြောင်းတာ (total / 60, total % 60) တွေမှာ သုံးပါတယ်။

Short-circuit guard တွေကတော့ backend code တိုင်းမှာ တွေ့ရပါတယ်: user != null && user.isActive()၊ list.isEmpty() || list.get(0) ... စသဖြင့်ပါ။ Overflow ကလည်း တကယ့် incident တွေ ဖြစ်ခဲ့ပါတယ် — count တစ်ခုခုက int ကျော်သွားလို့ အနုတ်ဂဏန်း ID တွေ၊ အနုတ် total တွေ ထွက်လာတာမျိုးပါ။ ဒါကြောင့် timestamp (System.currentTimeMillis()) နဲ့ file size တွေကို Java API တွေက long နဲ့ ပြန်ပေးတာပါ။ ပိုက်ဆံ တွက်တဲ့ code မှာ double အစား long cent ဒါမှမဟုတ် BigDecimal ကို သုံးတာက review မှာ မကြာခဏ တောင်းဆိုခံရတဲ့ အချက်ပါ။

အမှားများ

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

1. Cast ကို စားပြီးမှ လုပ်တာ

(double) (sum / count) က sum / count ကို int အဖြစ် အရင် စားပြီးမှ double ပြောင်းလို့ ဒသမ ပျောက်ပြီးသားပါ။ (double) sum / count ဒါမှမဟုတ် sum / (double) count လို့ ရေးပြီး တွက်ခင်မှာ cast လုပ်ပါ။

2. boolean condition ထဲမှာ = သုံးမိတာ

int အတွက် if (count = 5) က compile error ဖြစ်လို့ ချက်ချင်း သိရပါတယ်။ ဒါပေမယ့် boolean variable အတွက် if (isAdmin = true) က compile ဖြစ်ပြီး isAdmin ကို true assign လုပ်လိုက်လို့ အမြဲ true ဖြစ်နေပါတယ် — permission bug အစစ်ပါ။ if (isAdmin) လို့ပဲ ရေးပါ။

3. Guard ရဲ့ အစဉ် ပြောင်းပြန် ရေးတာ

name.length() > 0 && name != null က null စစ်ချက်ကို နောက်မှ ထားလို့ name null ဖြစ်ရင် ဘယ်ဘက်မှာတင် NullPointerException တက်ပါတယ်။ Short-circuit က ဘယ်ကနေ ညာ အလုပ်လုပ်လို့ ဘေးကင်းစေတဲ့ စစ်ချက်ကို အမြဲ ဘယ်ဘက်မှာ ထားပါ။ & ကို && နေရာမှာ သုံးရင်လည်း ဘက်နှစ်ဖက်လုံး တွက်လို့ အကာအကွယ် ပျောက်ပါတယ်။

Mini challenge

Mini challenge: ပစ္စည်းပို့ ဘူးတွက်စက်

Online shop တစ်ခုအတွက် item တွေကို ဘူးထဲ ထုပ်ပိုးတဲ့ တွက်ချက်မှုနဲ့ free shipping စစ်ချက်ကို operator တွေနဲ့ပဲ ရေးပါ (if statement မသုံးရ)။

  • boxesNeeded(items, perBox) — လိုအပ်တဲ့ ဘူးအရေအတွက်ကို integer division နဲ့ round up လုပ်ပြီး ပြန်ပေးရမယ်
  • lastBoxCount(items, perBox) — နောက်ဆုံးဘူးထဲက item အရေအတွက် (အပြည့်ဆိုရင် perBox) ကို % နဲ့ ? : သုံးပြီး ပြန်ပေးရမယ်
  • freeShipping(total, remoteArea) — total >= 50.0 ဖြစ်ပြီး remote area မဟုတ်မှ true
  • Item 23 ခု၊ ဘူးတစ်ဘူး 6 ခု၊ တစ်ခု 2.5 နဲ့ ရလဒ်တွေ print ထုတ်ရမယ်
Main.java
public class Main {
  static int boxesNeeded(int items, int perBox) {
    // TODO: round up without using double
    return 0;
  }

  static int lastBoxCount(int items, int perBox) {
    // TODO: use % and the ? : operator
    return 0;
  }

  static boolean freeShipping(double total, boolean remoteArea) {
    // TODO: combine two conditions
    return false;
  }

  public static void main(String[] args) {
    // TODO: 23 items, 6 per box, 2.5 each; print all results
  }
}

ရလဒ်: Program က ဒီ line ခြောက်ကြောင်းကို ထုတ်ရပါမယ်: Boxes: 4 Last box holds: 5 Total: 57.5 Free shipping: true Free shipping (remote): false Boxes for 24: 4

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

Round up လုပ်ဖို့ (items + perBox - 1) / perBox ကို သုံးပါ။ % ရလဒ် 0 ဆိုတာ နောက်ဆုံးဘူး ပြည့်နေတာပါ။ items * 2.5 က double ရပါတယ်။

အဖြေ ကြည့်ရန်
Main.java
public class Main {
  static int boxesNeeded(int items, int perBox) {
    return (items + perBox - 1) / perBox;
  }

  static int lastBoxCount(int items, int perBox) {
    int rest = items % perBox;
    return rest == 0 ? perBox : rest;
  }

  static boolean freeShipping(double total, boolean remoteArea) {
    return total >= 50.0 && !remoteArea;
  }

  public static void main(String[] args) {
    int items = 23;
    int perBox = 6;
    double total = items * 2.5;

    System.out.println("Boxes: " + boxesNeeded(items, perBox));
    System.out.println("Last box holds: " + lastBoxCount(items, perBox));
    System.out.println("Total: " + total);
    System.out.println("Free shipping: " + freeShipping(total, false));
    System.out.println("Free shipping (remote): " + freeShipping(total, true));
    System.out.println("Boxes for 24: " + boxesNeeded(24, perBox));
  }
}

Quick Quiz 1 / 3

System.out.println(7 / 2 * 2); က ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

int count = Integer.MAX_VALUE; count++; ပြီးရင် count ရဲ့ value က ဘာလဲ။

Quick Quiz 3 / 3

name က null ဖြစ်နိုင်ရင် ဘယ် condition က exception မတက်ဘဲ ဘေးကင်းလဲ။

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

  • int / int က int ပါ — ဒသမ လိုရင် စားခင်မှာ (double) cast လုပ်ပါ၊ % က အကြွင်းကို ပေးပါတယ်။
  • int overflow က error မပေးဘဲ wrap around ဖြစ်လို့ ကြီးတဲ့ value တွေအတွက် long ဒါမှမဟုတ် Math.addExact() ကို သုံးပါ။
  • && နဲ့ || က short-circuit လုပ်လို့ null စစ်ချက်ကို ဘယ်ဘက်မှာ ထားပါ။
  • + က ဘယ်ကနေ ညာ တွက်ပြီး string တွေ့တာနဲ့ concatenation ဖြစ်သွားလို့ ဂဏန်း ပေါင်းချင်ရင် ကွင်းခတ်ပါ။

Java Language Specification, Java SE 25 — Oracle

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

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

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

Operators | Thuta Learning