Thuta Learning
Java
IntermediateProgrammingအခြေခံ

Methods

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

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

  • static method တစ်ခုကို return type, name, parameter list နဲ့ body ပါအောင် ရေးနိုင်ဖို့
  • Parameter (method ထဲက နာမည်) နဲ့ argument (ခေါ်တဲ့အခါ ပို့တဲ့ value) ကို ခွဲခြားနိုင်ဖို့
  • Return type ရှိတဲ့ method မှာ လမ်းကြောင်းတိုင်း return ပြန်ရမယ်ဆိုတာနဲ့ void ရဲ့ အဓိပ္ပာယ်ကို ရှင်းပြနိုင်ဖို့
  • Primitive နဲ့ array/object argument တွေမှာ pass-by-value က ဘယ်လို အလုပ်လုပ်လဲ ကြိုတွက်နိုင်ဖို့

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

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

Method ဆိုတာ နာမည်ပေးထားတဲ့ ချက်ပြုတ်နည်း (recipe) တစ်ခုလိုပါပဲ။ ထမင်းကြော်နည်းကို တစ်ခါ ရေးထားပြီးရင် ချက်ချင်တိုင်း "ထမင်းကြော်" လို့ နာမည်နဲ့ ခေါ်လိုက်ရုံပါပဲ၊ အဆင့်တွေကို ထပ်ခါထပ်ခါ ပြန်ရေးစရာ မလိုပါဘူး။ Recipe ထဲကို ပါဝင်ပစ္စည်း (parameter) တွေ ထည့်ပေးရပြီး တချို့ recipe တွေက ဟင်းတစ်ပွဲ (return value) ပြန်ပေးပါတယ်။

တချို့ recipe တွေကတော့ ဘာမှ ပြန်မပေးဘဲ အလုပ်တစ်ခုပဲ လုပ်ပါတယ် — စားပွဲပြင်တာ၊ စာရင်းရေးတာလိုမျိုးပါ။ Java မှာ အဲဒီလို method တွေကို void လို့ ရေးပါတယ်။ main ထဲမှာ code အားလုံး စုပုံရေးမယ့်အစား အလုပ်တစ်ခုချင်းကို နာမည်ပေးထားတဲ့ method လေးတွေ ခွဲထုတ်လိုက်ရင် program ကို ဖတ်ရတာ၊ ပြင်ရတာ၊ စမ်းသပ်ရတာ အများကြီး လွယ်သွားပါတယ်။

Problem → Why → Solution

ပြဿနာ: copy-paste လုပ်ထားတဲ့ တွက်ချက်မှု

အောက်က main မှာ order သုံးခုရဲ့ စုစုပေါင်းကို တွက်ပြီး discount ချထားပါတယ်။ တွက်နည်း တစ်ခုတည်းကို သုံးခါ copy ကူးထားလို့ discount ကို 10% ကနေ 15% ပြောင်းချင်ရင် နေရာသုံးခုမှာ လိုက်ပြင်ရပါတယ်။ တတိယ order မှာ ပြင်ဖို့ မေ့သွားတာကို ကြည့်ပါ — compiler က ဘာ error မှ မပြပါဘူး၊ customer တစ်ယောက်ကပဲ ဈေးနှုန်းမှားနဲ့ ပေးရပါတယ်။

Main.java
public class Main {
  public static void main(String[] args) {
    int order1 = 20 * 3;
    order1 = order1 - order1 * 15 / 100;
    int order2 = 45 * 2;
    order2 = order2 - order2 * 15 / 100;
    int order3 = 12 * 10;
    order3 = order3 - order3 * 10 / 100; // forgot to update this copy
    System.out.println(order1 + " " + order2 + " " + order3);
  }
}

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

Rule တစ်ခု (discount တွက်နည်း) က နေရာသုံးခုမှာ ပြန့်ကျဲနေလို့ပါ။ static int discounted(int price, int quantity) ဆိုတဲ့ method တစ်ခုထဲ ရွှေ့လိုက်ရင် rule က တစ်နေရာတည်းမှာပဲ ရှိပြီး ပြင်ရင်လည်း ခေါ်ထားတဲ့ နေရာတိုင်းကို တစ်ပြိုင်နက် သက်ရောက်ပါတယ်။ Method နာမည်ကလည်း 20 * 3 - ... ထက် ဘာလုပ်နေလဲဆိုတာကို ပိုရှင်းရှင်း ပြောပြပါတယ်။

Core concept: method တစ်ခုရဲ့ အစိတ်အပိုင်းတွေနဲ့ pass-by-value

static int calculateTotal(int price, int quantity) ဆိုတဲ့ header ကို ဘယ်ကနေ ညာ ဖတ်ကြည့်ရအောင်။ static က object မဆောက်ဘဲ class ကနေ တိုက်ရိုက် ခေါ်လို့ရတယ်လို့ ဆိုလိုပါတယ် — main ကိုယ်တိုင်က static ဖြစ်လို့ အခုအဆင့်မှာ ကိုယ်ရေးတဲ့ method တွေကိုလည်း static ထားပါတယ်၊ object method တွေကိုတော့ နောက် lesson တွေမှာ တွေ့ရပါမယ်။ int က return type၊ calculateTotal က နာမည်၊ ကွင်းထဲက int price, int quantity က parameter list ပါ။ Parameter တစ်ခုစီမှာ type ကို ကိုယ်တိုင် ရေးရပါတယ်၊ int price, quantity လို့ ပေါင်းရေးလို့ မရပါဘူး။

Parameter ဆိုတာ method ထဲမှာ သုံးတဲ့ နာမည်ဖြစ်ပြီး argument ဆိုတာ ခေါ်တဲ့အခါ တကယ်ပို့လိုက်တဲ့ value ပါ။ calculateTotal(20, 3) မှာ 20 က price ထဲ၊ 3 က quantity ထဲ ရောက်ပါတယ် — နာမည်နဲ့ မဟုတ်ဘဲ အစဉ်အတိုင်း ချိတ်တာပါ။ Compiler က argument အရေအတွက်နဲ့ type ကို စစ်ပေမယ့် int နှစ်ခုကို အစဉ်ပြောင်းပြန် ပို့မိတာကိုတော့ မဖမ်းနိုင်ပါဘူး။

Return type က void မဟုတ်ရင် လမ်းကြောင်းတိုင်းမှာ အဲဒီ type ရဲ့ value ကို return ပြန်ရပါမယ်၊ မဟုတ်ရင် "missing return statement" compile error တက်ပါတယ်။ return က value ပြန်ပေးရုံသာမက method ကို ချက်ချင်း အဆုံးသတ်ပါတယ်။ void method တွေမှာတော့ return; တစ်ခုတည်းကို စောစော ထွက်ဖို့ပဲ သုံးပါတယ်။

Java က argument တိုင်းကို value နဲ့ ပို့ပါတယ် (pass-by-value)။ int လို primitive ဆိုရင် value ကို copy ကူးပေးလို့ method ထဲမှာ parameter ကို ပြောင်းလည်း caller ရဲ့ variable မပြောင်းပါဘူး။ Array နဲ့ object တွေမှာတော့ copy ကူးတာက object ကိုယ်တိုင် မဟုတ်ဘဲ object ရှိရာကို ညွှန်တဲ့ reference ပါ။ ဒါကြောင့် method က values[0] = ... လို object ထဲကို ပြင်ရင် caller လည်း မြင်ရပေမယ့် parameter ထဲကို object အသစ် assign လုပ်ရင်တော့ caller ဘက်က ဘာမှ မပြောင်းပါဘူး။ Java မှာ pass-by-reference လုံးဝ မရှိပါဘူး။ Method ထဲမှာ ကြေညာတဲ့ parameter နဲ့ variable တွေက local ဖြစ်ပြီး အဲဒီ call run နေတုန်းပဲ ရှိကာ method return ပြန်တာနဲ့ ပျောက်သွားပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: copy လုပ်ပြီး ပို့တယ်
  1. Argument တွေကို ဘယ်ကနေ ညာ တွက်
  2. Value (သို့) reference ကို copy ကူး
  3. Method body က copy တွေနဲ့ အလုပ်လုပ်
  4. return value ကို caller ဆီ ပြန်ပို့

Copy ကူးထားတဲ့ reference ကနေ object ထဲကို ပြင်ရင် caller မြင်ရပြီး parameter ကို ပြန် assign လုပ်ရင်တော့ မမြင်ရပါဘူး။

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

java
static ReturnType methodName(Type param1, Type param2) {
  // work with param1 and param2
  return value; // must match ReturnType
}

static void doSomething(Type param) {
  // side effect only, nothing to return
}

ReturnType result = methodName(arg1, arg2);

Return type, method နာမည်၊ ကွင်းထဲမှာ type ပါတဲ့ parameter တွေ ဆက်တိုက် ရေးပါတယ်။ Value ပြန်ပေးတဲ့ method ကို ခေါ်ရင် ရလဒ်ကို variable ထဲ သိမ်းတာ ဒါမှမဟုတ် တခြား expression ထဲမှာ တိုက်ရိုက် သုံးတာ လုပ်ပါ။ void method ကိုတော့ statement တစ်ခုအဖြစ်ပဲ ခေါ်ပါတယ်။

Example: value ပြန်တဲ့ method, void method နဲ့ pass-by-value

ဒီ example မှာ method လေးခု ပါပါတယ်။ greetUser() နဲ့ calculateTotal() က method အမျိုးအစား နှစ်မျိုးကို ပြပြီး tryToReset() နဲ့ doubleFirst() က pass-by-value ရဲ့ မျက်နှာနှစ်ဖက်ကို ပြပါတယ်။ Run မလုပ်ခင် count နဲ့ prices ဘာ print ထုတ်မလဲ ကိုယ်တိုင် အရင် ခန့်မှန်းကြည့်ပါ။

Main.java
import java.util.Arrays;

public class Main {
  static void greetUser(String name) {
    System.out.println("Hello, " + name + "!");
  }

  static int calculateTotal(int price, int quantity) {
    return price * quantity;
  }

  static void tryToReset(int count) {
    count = 0;
  }

  static void doubleFirst(int[] values) {
    values[0] = values[0] * 2;
  }

  public static void main(String[] args) {
    greetUser("Aung");
    int total = calculateTotal(20, 3);
    System.out.println("Total: " + total);

    int count = 5;
    tryToReset(count);
    System.out.println("count is still " + count);

    int[] prices = {100, 250};
    doubleFirst(prices);
    System.out.println("prices = " + Arrays.toString(prices));
  }
}

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

  1. void methodstatic void greetUser(String name) {

    void ဆိုတော့ ဘာမှ return မပြန်ပါဘူး၊ အလုပ်က print ထုတ်ရုံပါပဲ။ name က String parameter ဖြစ်လို့ greetUser(42) လို ခေါ်ရင် compile error တက်ပါတယ်။

  2. Returning a valuereturn price * quantity;

    int နှစ်ခု မြှောက်ရင် int ရလို့ return type int နဲ့ ကိုက်ပါတယ်။ Caller က int total = calculateTotal(20, 3); နဲ့ 60 ကို ဖမ်းယူပါတယ်။

  3. Changing a primitive copycount = 0;

    ဒီ count က main ထဲက count ရဲ့ copy ပါ၊ နာမည်တူနေတာ တိုက်ဆိုင်မှုပဲ ဖြစ်ပါတယ်။ Copy ကို 0 ပြောင်းလည်း main ရဲ့ count က 5 အတိုင်း ကျန်ပါတယ်။

  4. Changing through a copied referencevalues[0] = values[0] * 2;

    values က prices array ကို ညွှန်တဲ့ reference ရဲ့ copy ပါ။ Copy နှစ်ခုလုံးက array တစ်ခုတည်းကို ညွှန်နေလို့ ဒီမှာ ပြင်တာကို main က [200, 250] အဖြစ် မြင်ရပါတယ်။

  5. Calling from maintryToReset(count);

    ဒီ call မှာ count ရဲ့ value 5 ကိုပဲ ပို့လိုက်တာပါ၊ variable ကိုယ်တိုင်ကို မဟုတ်ပါဘူး။ ဒါကြောင့် နောက် line က "count is still 5" လို့ print ထုတ်ပါတယ်။

You should see
Hello, Aung!
Total: 60
count is still 5
prices = [200, 250]

Reference copy ကို ပြန် assign လုပ်ရင် ဘာဖြစ်လဲ

Array parameter ထဲကို array အသစ် assign လုပ်လိုက်ရင် method ရဲ့ local copy ကပဲ နေရာသစ်ကို ညွှန်သွားပါတယ်။ Caller ရဲ့ variable ကတော့ array အဟောင်းကိုပဲ ဆက်ညွှန်နေပါတယ်။ အောက်က replace() နဲ့ fill() ကို နှိုင်းယှဉ်ကြည့်ပါ — ဒါက "Java is pass-by-reference" ဆိုတဲ့ အထင်မှားကို ချေဖျက်တဲ့ အရှင်းဆုံး စမ်းသပ်မှုပါ။

text
Two references, one array
-------------------------
main:   data   ----+
                   |
                   v
                 [ 0 , 0 ]   <-- fill() writes here: caller sees it
                   ^
fill:   values ----+

replace: values ----> [ 9 , 9 ]  (new array, caller never sees it)
Main.java
import java.util.Arrays;

public class Main {
  static void replace(int[] values) {
    values = new int[] {9, 9}; // the local copy now points to a new array
    values[0] = 1;
  }

  static void fill(int[] values) {
    values[0] = 1; // follows the copied reference to the caller's array
  }

  public static void main(String[] args) {
    int[] data = {0, 0};
    replace(data);
    System.out.println("after replace: " + Arrays.toString(data));
    fill(data);
    System.out.println("after fill:    " + Arrays.toString(data));
  }
}

Output က after replace: [0, 0] နဲ့ after fill: [1, 0] ဖြစ်ပါတယ်။ String နဲ့ Integer လို immutable object တွေမှာ ပြင်လို့ရတဲ့ method မရှိတဲ့အတွက် method ထဲက ဘယ်လိုပဲ လုပ်လုပ် caller ရဲ့ value မပြောင်းနိုင်ပါဘူး — ပြောင်းထားတဲ့ ရလဒ်ကို return ပြန်ပေးရပါတယ်။

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

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

Production code မှာ method တစ်ခုချင်းက အလုပ်တစ်ခုတည်းကို ကိုင်ပါတယ်: calculateShipping(), isValidEmail(), formatInvoiceNumber() စသဖြင့်ပါ။ ဒီလို method သေးသေးလေးတွေက unit test ရေးဖို့ အလွန်လွယ်ပါတယ် — input တစ်ခု ပို့လိုက်၊ return value ကို စစ်လိုက်ရုံပါပဲ။ Field ဒါမှမဟုတ် database ကို မထိဘဲ parameter ကနေ return value ပဲ တွက်တဲ့ "pure" method တွေက ပိုတောင် စမ်းရလွယ်ပါတယ်။

Pass-by-value ကို နားလည်ထားတာက code review မှာ အရေးကြီးပါတယ်။ List ဒါမှမဟုတ် array တစ်ခုကို helper method ဆီ ပို့လိုက်ရင် အဲဒီ method က ထဲက data ကို ပြင်နိုင်ပါတယ်၊ ဒါကို caller က မမျှော်လင့်ထားရင် bug ရှာရခက်ပါတယ်။ ဒါကြောင့် team အများစုက method နာမည်ကနေ ပြင်မပြင် သိသာအောင် ရေးပါတယ် (sort လုပ်ပြီး ပြင်တာဆို sortInPlace၊ copy အသစ် ပြန်ပေးတာဆို sorted) ပြီး ပြင်စရာမလိုရင် data အသစ်ကို return ပြန်တဲ့ ပုံစံကို ပိုကြိုက်ကြပါတယ်။ Java ရဲ့ standard library ကိုယ်တိုင်လည်း Arrays.sort() (array ကို ပြင်တယ်) နဲ့ stream().sorted() (အသစ်ပြန်ပေးတယ်) လို ဒီနှစ်မျိုးလုံးကို သုံးထားပါတယ်။

အမှားများ

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

1. လမ်းကြောင်းတစ်ခုမှာ return မေ့တာ

static int sign(int n) { if (n > 0) return 1; else if (n < 0) return -1; } က "missing return statement" error ပေးပါတယ်။ n == 0 ဖြစ်တဲ့ လမ်းကြောင်းမှာ return မရှိလို့ပါ — compiler က else if နှစ်ခုက ဖြစ်နိုင်ခြေ အားလုံးကို ဖုံးတယ်လို့ မတွက်ပေးပါဘူး။ နောက်ဆုံးမှာ return 0; ထည့်ပါ။

2. Return value ကို လွှင့်ပစ်မိတာ

calculateTotal(20, 3); လို့ statement တစ်ခုတည်း ရေးရင် compile ဖြစ်ပေမယ့် 60 က ဘယ်မှာမှ မသိမ်းဘဲ ပျောက်သွားပါတယ်။ name.toUpperCase(); မှာလည်း ဒီအတိုင်းပဲ — String မပြောင်းပါဘူး။ ရလဒ်ကို variable ထဲ သိမ်းပါ။

3. Parameter ကို ပြန် assign လုပ်ရင် caller ပြောင်းမယ်ထင်တာ

tryToReset(count) ဒါမှမဟုတ် values = new int[] {...} လို ရေးပြီး caller ဘက်မှာ ပြောင်းသွားမယ်လို့ မျှော်လင့်တာပါ။ Parameter တွေက copy တွေပါ၊ value အသစ်လိုရင် return ပြန်ပြီး int count = reset(); လို caller ကပဲ assign လုပ်ပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Method နာမည်ကို verb နဲ့ စပါ (calculateTotal, sendEmail)၊ boolean ပြန်တဲ့ method ကို isValid, hasStock လို မေးခွန်းပုံစံ ပေးပါ။
  • Method တစ်ခုက screen တစ်ခုထက် ရှည်လာရင် အလုပ်နှစ်ခု ရောနေတာ များပါတယ်၊ ခွဲထုတ်ပါ။
  • Argument ပြင်တဲ့ method ဆိုရင် နာမည် ဒါမှမဟုတ် comment မှာ ရှင်းရှင်း ဖော်ပြပါ၊ မပြင်ဘဲ ရလဒ်အသစ် return ပြန်နိုင်ရင် အဲဒီလမ်းကို ရွေးပါ။

Mini challenge

Mini challenge: အမှတ် helper method များ

အတန်းတစ်တန်းရဲ့ အမှတ်တွေအတွက် method သုံးခု ရေးပြီး bonus မပေးခင်နဲ့ ပေးပြီးနောက် ပျမ်းမျှနဲ့ grade ကို print ထုတ်ပါ။

  • average(int[] scores) က double ပြန်ရမယ်၊ array အလွတ်ဆိုရင် 0.0 ပြန်ရမယ်
  • letterGrade(double average) က 80+ = A, 65+ = B, 50+ = C, ကျန်တာ F ပြန်ရမယ်
  • curve(int[] scores, int bonus) က void ဖြစ်ပြီး array ကို တိုက်ရိုက် ပြင်ရမယ်၊ 100 ထက် မကျော်ရ
  • main မှာ {72, 95, 58} ကို သုံးပြီး curve မလုပ်ခင်နဲ့ လုပ်ပြီးနောက် ရလဒ်ကို print ထုတ်ရမယ်
Main.java
import java.util.Arrays;

public class Main {
  static double average(int[] scores) {
    // TODO: return 0.0 for an empty array, otherwise sum / length as a double
  }

  static String letterGrade(double average) {
    // TODO: A for 80+, B for 65+, C for 50+, otherwise F
  }

  static void curve(int[] scores, int bonus) {
    // TODO: add bonus to every score in place, never above 100
  }

  public static void main(String[] args) {
    int[] scores = {72, 95, 58};
    // TODO: print average + grade, curve by 10, print the array, average + grade
  }
}

ရလဒ်: အောက်ပါအတိုင်း လေးကြောင်း ထွက်ရပါမယ်: Before: 75.0 -> B Curved: [82, 100, 68] After: 83.33 -> A Empty class: 0.0

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

int sum ကို length နဲ့ စားရင် ဒသမ ပျောက်သွားလို့ (double) sum / scores.length လို့ cast လုပ်ပါ။ curve() ထဲမှာ Math.min(100, score + bonus) ကို သုံးပြီး scores[i] ထဲ ပြန်ရေးပါ — array က copy ကူးထားတဲ့ reference ကနေ ပြင်တာမို့ main ကလည်း မြင်ရပါမယ်။

အဖြေ ကြည့်ရန်
Main.java
import java.util.Arrays;

public class Main {
  static double average(int[] scores) {
    if (scores.length == 0) {
      return 0.0;
    }
    int sum = 0;
    for (int score : scores) {
      sum += score;
    }
    return (double) sum / scores.length;
  }

  static String letterGrade(double average) {
    if (average >= 80) return "A";
    if (average >= 65) return "B";
    if (average >= 50) return "C";
    return "F";
  }

  static void curve(int[] scores, int bonus) {
    for (int i = 0; i < scores.length; i++) {
      scores[i] = Math.min(100, scores[i] + bonus);
    }
  }

  static double round2(double value) {
    return Math.round(value * 100) / 100.0;
  }

  public static void main(String[] args) {
    int[] scores = {72, 95, 58};
    double before = average(scores);
    System.out.println("Before: " + round2(before) + " -> " + letterGrade(before));

    curve(scores, 10);
    double after = average(scores);
    System.out.println("Curved: " + Arrays.toString(scores));
    System.out.println("After: " + round2(after) + " -> " + letterGrade(after));
    System.out.println("Empty class: " + average(new int[0]));
  }
}

Quick Quiz 1 / 3

static void bump(int n) { n = n + 1; } ရှိပြီး int x = 4; bump(x); System.out.println(x); ဆိုရင် ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

ဘယ် method က "missing return statement" compile error ပေးမလဲ။

Quick Quiz 3 / 3

static void clear(int[] a) { a[0] = 0; a = new int[3]; } ရှိပြီး int[] d = {7, 8}; clear(d); ပြီးရင် Arrays.toString(d) က ဘာလဲ။

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

  • Method header က return type, နာမည်နဲ့ type ပါတဲ့ parameter list ပါဝင်ပြီး argument တွေကို အစဉ်အတိုင်း ချိတ်ပါတယ်။
  • void မဟုတ်တဲ့ method က လမ်းကြောင်းတိုင်းမှာ return ပြန်ရပါမယ်၊ return က method ကို ချက်ချင်း အဆုံးသတ်ပါတယ်။
  • Java က အမြဲ pass-by-value ပါ — primitive ဆို value ကို၊ object/array ဆို reference ကို copy ကူးပါတယ်။
  • Reference copy ကနေ object ထဲကို ပြင်ရင် caller မြင်ရပြီး parameter ကို ပြန် assign လုပ်ရင် မမြင်ရပါဘူး။

Java Language Specification, Java SE 25 — Oracle

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

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

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

Methods | Thuta Learning