נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
7
חלקPart 2: מתודות ומערכיםMethods & arrays כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

מתודות, overloading ו-pass-by-valueMethods, overloading & pass-by-value

הגדרת מתודות, פרמטרים וערך החזרה, overloading, ומה באמת עובר למתודה: עותק של הערך, גם כשהערך הוא הפניה.

Defining methods, parameters and return values, overloading, and what really reaches a method: a copy of the value, even when the value is a reference.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • פרמטר: משתנה בהגדרת המתודה שמקבל ערך מבחוץ:parameter: A variable in the method's definition that receives a value from outside:
  • ארגומנט: הערך שנותנים לפרמטר בקריאה:argument: The value given to a parameter in a call:
  • מתודה היא פעולה עם שם.A method is a named action.
  • בהגדרה: טיפוס ההחזרה, שם ופרמטרים.In the definition: the return type, a name and parameters.
  • מתודה void לא מחזירה ערך.A void method returns nothing.
  • מתודות יכולות לקרוא זו לזו, וכל קריאה מקבלת מסגרת משלה.Methods can call each other, and each call gets its own frame.
  • Java בוחרת איזו גרסה להפעיל לפי הטיפוסים ומספר הארגומנטים.Java picks which version to run from the types and the number of arguments.
  • המתודה מקבלת עותק של כל ארגומנט.The method gets a copy of every argument.
  • x הוא עותק של 1, ולכן n לא משתנה.x is a copy of 1, so n doesn't change.

מונחיםTerms

  • מתודהmethod: פעולה עם שם שמוגדרת בתוך מחלקה, כמו main או println. קוראים לה עם סוגריים.A named action defined inside a class, like main or println. You call it with parentheses.
  • פרמטרparameter: משתנה בהגדרת המתודה שמקבל ערך מבחוץ: ב-int add(int a, int b), a ו-b הם פרמטרים.A variable in the method's definition that receives a value from outside: in int add(int a, int b), a and b are parameters.
  • ארגומנטargument: הערך שנותנים לפרמטר בקריאה: ב-add(2, 3) הארגומנטים הם 2 ו-3.The value given to a parameter in a call: in add(2, 3) the arguments are 2 and 3.
  • ערך החזרהreturn value: התשובה שמתודה מחזירה עם return. הטיפוס שלה כתוב לפני שם המתודה.The answer a method hands back with return. Its type is written before the method name.
  • voidvoid: מתודה void לא מחזירה ערך: היא עושה משהו (למשל מדפיסה) וחוזרת.A void method returns no value: it does something (like printing) and goes back.
  • מסגרת (frame)stack frame: בכל קריאה למתודה נוצרת מסגרת חדשה ב-stack עם המשתנים שלה. כשהמתודה חוזרת, המסגרת נמחקת.Every method call creates a new frame on the stack holding its variables. When the method returns, the frame is removed.
  • העמסה (overloading)overloading: כמה מתודות עם אותו שם ופרמטרים שונים. Java בוחרת לפי הארגומנטים: max(int, int) מול max(double, double).Several methods with the same name and different parameters. Java picks by the arguments: max(int, int) vs max(double, double).
  • העברה לפי ערךpass-by-value: ב-Java כל ארגומנט מועתק לפרמטר. למספר: המתודה לא יכולה לשנות את המשתנה של הקורא. להפניה: מועתקת ההפניה, ושתיהן מצביעות על אותו עצם.In Java every argument is copied into the parameter. For a number: the method can't change the caller's variable. For a reference: the reference is copied, and both point to the same object.
  • הפניהreference: מה שיושב בקופסה של משתנה מסוג עצם: לא העצם עצמו, אלא "כתובת" שמצביעה עליו ב-heap.What sits in the box of an object-type variable: not the object itself, but an "address" pointing to it on the heap.
  • תחום (scope)scope: האזור בקוד שבו משתנה קיים: משתנה שהוגדר בתוך מתודה או בלוק נעלם בסוף שלהם.The part of the code where a variable exists: a variable declared in a method or block disappears at its end.
  • פרמטרparameter: משתנה בהגדרת המתודה שמקבל ערך מבחוץ: ב-int add(int a, int b), a ו-b הם פרמטרים.A variable in the method's definition that receives a value from outside: in int add(int a, int b), a and b are parameters.
  • ארגומנטargument: הערך שנותנים לפרמטר בקריאה: ב-add(2, 3) הארגומנטים הם 2 ו-3.The value given to a parameter in a call: in add(2, 3) the arguments are 2 and 3.
  • ערך החזרהreturn value: התשובה שמתודה מחזירה עם return. הטיפוס שלה כתוב לפני שם המתודה.The answer a method hands back with return. Its type is written before the method name.
  • voidvoid: מתודה void לא מחזירה ערך: היא עושה משהו (למשל מדפיסה) וחוזרת.A void method returns no value: it does something (like printing) and goes back.
  • מסגרת (frame)stack frame: בכל קריאה למתודה נוצרת מסגרת חדשה ב-stack עם המשתנים שלה. כשהמתודה חוזרת, המסגרת נמחקת.Every method call creates a new frame on the stack holding its variables. When the method returns, the frame is removed.
  • העמסה (overloading)overloading: כמה מתודות עם אותו שם ופרמטרים שונים. Java בוחרת לפי הארגומנטים: max(int, int) מול max(double, double).Several methods with the same name and different parameters. Java picks by the arguments: max(int, int) vs max(double, double).
  • העברה לפי ערךpass-by-value: ב-Java כל ארגומנט מועתק לפרמטר. למספר: המתודה לא יכולה לשנות את המשתנה של הקורא. להפניה: מועתקת ההפניה, ושתיהן מצביעות על אותו עצם.In Java every argument is copied into the parameter. For a number: the method can't change the caller's variable. For a reference: the reference is copied, and both point to the same object.
  • תחום (scope)scope: האזור בקוד שבו משתנה קיים: משתנה שהוגדר בתוך מתודה או בלוק נעלם בסוף שלהם.The part of the code where a variable exists: a variable declared in a method or block disappears at its end.

Cheatsheet

מגדירים וקוראיםDefine and call
int total = add(2, 3);
System.out.println(total);
System.out.println(add(10, total));
void ומתודה שקוראת למתודהvoid, and a method calling a method
line(3);
line(5);
Overloading: אותו שם, פרמטרים שוניםOverloading: same name, different parameters
System.out.println(describe(7));
System.out.println(describe(7.5));
System.out.println(describe("seven"));
System.out.println(describe(3, 4));
Pass-by-value, בזיכרוןPass-by-value, in memory
int n = 1;
int[] a = {1, 2, 3};
change(n, a);
System.out.println(n + " " + a[0]);

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do

נבנה על ידיBuilt by Ariel Yohimovich