נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
מתודות, overloading ו-pass-by-valueMethods, overloading & pass-by-value
מפגש 7: הגדרת מתודות, פרמטרים וערך החזרה, overloading, ומה באמת עובר למתודה: עותק של הערך, גם כשהערך הוא הפניה.Session 7: 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.
הגדרת מתודות, פרמטרים וערך החזרה, 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. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- מגדירים וקוראיםDefine and call
- void ומתודה שקוראת למתודהvoid, and a method calling a method
- Overloading: אותו שם, פרמטרים שוניםOverloading: same name, different parameters
- Pass-by-value, בזיכרוןPass-by-value, in memory
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- פרמטר: משתנה בהגדרת המתודה שמקבל ערך מבחוץ:parameter: A variable in the method's definition that receives a value from outside:
- ארגומנט: הערך שנותנים לפרמטר בקריאה:argument: The value given to a parameter in a call:
- ערך החזרה: התשובה שמתודה מחזירה עם
return.return value: The answer a method hands back with return. - void: מתודה
void לא מחזירה ערך:void: A void method returns no value: - מסגרת (frame): בכל קריאה למתודה נוצרת מסגרת חדשה ב-stack עם המשתנים שלה.stack frame: Every method call creates a new frame on the stack holding its variables.
- העמסה (overloading): כמה מתודות עם אותו שם ופרמטרים שונים.overloading: Several methods with the same name and different parameters.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
מגדירים וקוראיםDefine and call
מגדירים וקוראיםDefine and call
- מתודה היא פעולה עם שם.A method is a named action.
- בהגדרה: טיפוס ההחזרה, שם ופרמטרים.In the definition: the return type, a name and parameters.
return מחזיר את התשובה.return hands back the answer.
מתודה היא פעולה עם שם. בהגדרה: טיפוס ההחזרה, שם ופרמטרים. return מחזיר את התשובה. בסימולציה רואים את Java קופצת לתוך המתודה, ואת הקופסאות a ו-b שנוצרות שם.A method is a named action. In the definition: the return type, a name and parameters. return hands back the answer. In the simulation you see Java jump into the method, and the boxes a and b created there.נושאTopic 3
void ומתודה שקוראת למתודהvoid, and a method calling a method
void ומתודה שקוראת למתודהvoid, and a method calling a method
- מתודה void לא מחזירה ערך.A void method returns nothing.
- מתודות יכולות לקרוא זו לזו, וכל קריאה מקבלת מסגרת משלה.Methods can call each other, and each call gets its own frame.
מתודה void לא מחזירה ערך. מתודות יכולות לקרוא זו לזו, וכל קריאה מקבלת מסגרת משלה.A void method returns nothing. Methods can call each other, and each call gets its own frame.נושאTopic 4
Overloading: אותו שם, פרמטרים שוניםOverloading: same name, different parameters
Overloading: אותו שם, פרמטרים שוניםOverloading: same name, different parameters
- Java בוחרת איזו גרסה להפעיל לפי הטיפוסים ומספר הארגומנטים.Java picks which version to run from the types and the number of arguments.
Java בוחרת איזו גרסה להפעיל לפי הטיפוסים ומספר הארגומנטים.Java picks which version to run from the types and the number of arguments.נושאTopic 5
Pass-by-value, בזיכרוןPass-by-value, in memory
Pass-by-value, בזיכרוןPass-by-value, in memory
- המתודה מקבלת עותק של כל ארגומנט.The method gets a copy of every argument.
x הוא עותק של 1, ולכן n לא משתנה.x is a copy of 1, so n doesn't change.arr הוא עותק של ההפניה, ולכן הוא מצביע על אותו מערך כמו a, והשינוי ב-arr[0] נראה גם ב-main.arr is a copy of the reference, so it points to the same array as a, and the change to arr[0] shows up in main too.
המתודה מקבלת עותק של כל ארגומנט. x הוא עותק של 1, ולכן n לא משתנה. arr הוא עותק של ההפניה, ולכן הוא מצביע על אותו מערך כמו a, והשינוי ב-arr[0] נראה גם ב-main. צפו בחצים.The method gets a copy of every argument. x is a copy of 1, so n doesn't change. arr is a copy of the reference, so it points to the same array as a, and the change to arr[0] shows up in main too. Watch the arrows.נושאTopic 6
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 7: מתודות לספרShelf, step 7: book methods
כתבו static String card(String title, int year, int stars) שמחזירה Dune (1965) **** (כוכב לכל דירוג), ו-static double average(int a, int b, int c). ב-main הדפיסו שני כרטיסים ואחר כך Average: 3.67 (ממוצע של 4, 3, 4) עם printf.Write static String card(String title, int year, int stars) returning Dune (1965) **** (a star per point), and static double average(int a, int b, int c). In main print two cards and then Average: 3.67 (the average of 4, 3, 4) with printf.פרמטר: משתנה בהגדרת המתודה שמקבל ערך מבחוץ:parameter: A variable in the method's definition that receives a value from outside:
המונחים של המפגשThis session's termsארגומנט: הערך שנותנים לפרמטר בקריאה:argument: The value given to a parameter in a call:
המונחים של המפגשThis session's termsערך החזרה: התשובה שמתודה מחזירה עם return.return value: The answer a method hands back with return.
המונחים של המפגשThis session's termsvoid: מתודה void לא מחזירה ערך:void: A void method returns no value:
המונחים של המפגשThis session's termsמסגרת (frame): בכל קריאה למתודה נוצרת מסגרת חדשה ב-stack עם המשתנים שלה.stack frame: Every method call creates a new frame on the stack holding its variables.
המונחים של המפגשThis session's termsהעמסה (overloading): כמה מתודות עם אותו שם ופרמטרים שונים.overloading: Several methods with the same name and different parameters.
המונחים של המפגשThis session's termsמתודה היא פעולה עם שם.A method is a named action.
מגדירים וקוראיםDefine and callבהגדרה: טיפוס ההחזרה, שם ופרמטרים.In the definition: the return type, a name and parameters.
מגדירים וקוראיםDefine and callreturn מחזיר את התשובה.return hands back the answer.
מגדירים וקוראיםDefine and callמתודה void לא מחזירה ערך.A void method returns nothing.
void ומתודה שקוראת למתודהvoid, and a method calling a methodמתודות יכולות לקרוא זו לזו, וכל קריאה מקבלת מסגרת משלה.Methods can call each other, and each call gets its own frame.
void ומתודה שקוראת למתודהvoid, and a method calling a methodJava בוחרת איזו גרסה להפעיל לפי הטיפוסים ומספר הארגומנטים.Java picks which version to run from the types and the number of arguments.
Overloading: אותו שם, פרמטרים שוניםOverloading: same name, different parametersהמתודה מקבלת עותק של כל ארגומנט.The method gets a copy of every argument.
Pass-by-value, בזיכרוןPass-by-value, in memoryx הוא עותק של 1, ולכן n לא משתנה.x is a copy of 1, so n doesn't change.
Pass-by-value, בזיכרוןPass-by-value, in memoryarr הוא עותק של ההפניה, ולכן הוא מצביע על אותו מערך כמו a, והשינוי ב-arr[0] נראה גם ב-main.arr is a copy of the reference, so it points to the same array as a, and the change to arr[0] shows up in main too.
Pass-by-value, בזיכרוןPass-by-value, in memoryמדף, צעד 7: מתודות לספרShelf, step 7: book methods
הצעד של היום בפרויקט מדףToday's step in the Shelf projectמה הטיפוס של התשובה ש-static double half(int n) מחזירה?What type of answer does static double half(int n) return?
intintdoubledoublevoidvoidhalfhalf
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.doubledouble
נכון: הטיפוס שלפני השם.Right: the type before the name.מה ידפיס הקוד?What does it print?
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.55
נכון: inc קיבל עותק של 5.Right: inc got a copy of 5.איזו מתודה לא יכולה לחיות ליד static int f(int a) באותה מחלקה?Which method can not sit next to static int f(int a) in the same class?
static int f(int a, int b)static int f(int a, int b)static int f(double a)static int f(double a)static double f(int x)static double f(int x)static int f(String s)static int f(String s)
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.static double f(int x)static double f(int x)
נכון: רק טיפוס ההחזרה שונה, וזה לא מספיק. Java לא תדע איזו לבחור.Right: only the return type differs, which isn't enough. Java couldn't tell which to pick.מתודה שמחשבת ריבועA method that squares
- כתבו
static int square(int n) שמחזירה את n בריבוע.Write static int square(int n) that returns n squared.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.האם זוגי?Is it even?
- כתבו
static boolean isEven(int n).Write static boolean isEven(int n).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.המקסימום מבין שלושהThe biggest of three
- כתבו
static int max(int a, int b), ואז static int max(int a, int b, int c) שמשתמשת בראשונה (overloading).Write static int max(int a, int b), then static int max(int a, int b, int c) that uses the first one (overloading).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מחיר אחרי הנחהPrice after discount
- כתבו
static double discount(double price, int percent) שמחזירה את המחיר אחרי ההנחה.Write static double discount(double price, int percent) returning the price after the discount.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: ההחלפה שלא עבדהBug hunter: the swap that didn't
swap אמורה להחליף בין שני המספרים, אבל main מדפיס 1 2.swap should swap two numbers, but main prints 1 2.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 7: מתודות לספרShelf, step 7: book methods
- כתבו
static String card(String title, int year, int stars) שמחזירה Dune (1965) **** (כוכב לכל דירוג), ו-static double average(int a, int b, int c).Write static String card(String title, int year, int stars) returning Dune (1965) **** (a star per point), and static double average(int a, int b, int c).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.ספרות של מספרA number's digits
- כתבו
static int digitSum(int n) (סכום הספרות, בלולאה) ו-static int digitCount(int n) (כמה ספרות).Write static int digitSum(int n) (the sum of the digits, in a loop) and static int digitCount(int n) (how many digits).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.ראשוני?Prime?
- כתבו
static boolean isPrime(int n) (ראשוני:Write static boolean isPrime(int n) (prime:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מה למדנוWhat we learned
- פרמטר: משתנה בהגדרת המתודה שמקבל ערך מבחוץ:parameter: A variable in the method's definition that receives a value from outside:
- מתודה היא פעולה עם שם.A method is a named action.
- מתודה void לא מחזירה ערך.A void method returns nothing.
- Java בוחרת איזו גרסה להפעיל לפי הטיפוסים ומספר הארגומנטים.Java picks which version to run from the types and the number of arguments.
- המתודה מקבלת עותק של כל ארגומנט.The method gets a copy of every argument.
- מדף, צעד 7: מתודות לספרShelf, step 7: book methods
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.