מעבר והחזרת ערכים על ידי התייחסות
הוצג
7:25
יצירת מחלקות
הנה סטודנט, מאוחסן כשלושה משתנים נפרדים: שם, שנה ומדורג. זה עובד. כעת הוסף אלפי סטודנטים. תצטרך שלושת אלפים משתנים…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| NullPointerException/nʌl ˈpɔɪntə ekˈsepʃn/ | שגיאת נקודה ריקה |
Mutating an object differs from reassigning a parameter
- A caller refers to a Student with score 10. The method executes s.addScore(5); then s = new Student("Other");
- The score of the original shared object becomes 15. Reassigning the local parameter does not change the caller variable, because Java copies the reference value into the parameter.

Passing an object to a method
- Java copies the reference value into the method parameter; it does not copy the object. The method's parameter points at the same object as the caller's variable.
- So changes the method makes to the object are visible to the caller. Both primitive and reference arguments are passed by value; the copied reference can still reach a shared object.
Primitives vs. objects as arguments
- A primitive argument is copied — the method can't change the caller's variable. For an object argument, the copied reference reaches the same object — the method can mutate that object.
changeInt(x)can't alterx;student.addScore(10)does change that student. Ask whether the method mutates the shared object or reassigns only its local parameter.
Returning an object
- A method can return a reference to an object — the caller then shares it.
public Student topStudent() { return best; }hands back a reference tobest. - Returning
bestshares an existing object. In contrast,return new Student("Copy");constructs a new object and returns its reference; a method can also returnnull. Read the return expression before deciding whether state is shared.
העתקת ייחוס, אובייקט משותף; העתקה של ערך פרמיטיבי
ערך הניקוד של האובייקט משתנה; המשתנה x הפרמיטיבי אינו משתנה.
כשעוברים אובייקט למטהודה, המטהודה מקבלת...
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Java מעתיקה את ערך הייחוס לתוך משתנה פרמטר נפרד. שני הייחוסים יכולים להגיע לאותו אובייקט; השבת ערך בפרמטר אינה מחליפה את משתנה הקורא.
מטהודה יכולה לשנות משתנה int של הקורא על ידי שינוי הפרמטר שלה.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
ערכים פרמיטיביים מועתקים — משתנה int של הקורא נשאר ללא שינוי.
השערת שיטת אינסטנץ דרך ייחוס null גורמת ל...
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
null אינו מצביע על שום אובייקט, ולכן אין מה לקרוא עליו את המטהודה.
מטהודה יכולה להחזיר התייחסות לאובייקט, שהקורא ישתמש בה מאוחר יותר.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
שיטה יכולה להחזיר ייחוס לאובייקט קיים ולשתף אותו עם הקורא. שיטה אחרת יכולה לבנות ולהחזיר אובייקט חדש במקום זאת.
כדי להשתמש בבטחה בהתייחסות שעשויה להיות נולל, עליך לבדוק תחילה אם...
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
הגן על null לפני ביצוע שיטה עליו.
כל שיטה שמחזירה ייחוס Student חייבת להחזיר אובייקט שקיים מראש.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
שגוי: return new Student("Other"); בונה ומחזיר אובייקט חדש.
The null reference
- A reference that points at no object is
null. Calling an instance method throughnullthrows a NullPointerException 空指针异常. - Returning
null(e.g. "not found") is common — the caller must check for it. Guard withif (s != null) { s.addScore(10); }before using a possibly-null reference.
Passing an object copies a reference value — the method can change the caller's object; passing a primitive copies the value — it can't. So student.addScore(10) mutates the caller's object, but changeInt(x) leaves the caller's x untouched. And calling an instance method through a null reference throws a NullPointerException — check != null first when a reference might be null.
After the two statements, what is the original caller-referenced Student score?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
The first statement mutates the original object to 15. The later local reassignment does not replace the caller reference.
שיטה מחזירה new Student("Other"); אך הקורא מתעלם מהתוצאה. מה קורה למשתנה Student הקיים בקורא?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
החזרת ייחוס אינה מצבנת אותו במשתנה הקורא. המשתנה הקיים עדיין מצביע על האובייקט המקורי שלו.
Reference vs. value:
void addTen(Student s) { s.addScore(10); }— changes the passed object (shared reference).void addTen(int x) { x += 10; }— the caller'sintis unchanged (copied value).- Both parameter values are copied; only the copied reference reaches shared mutable state.
Carry the reasoning to a new case
- Compare
return s;withreturn new Student("Other");: the first returns the supplied reference; the second returns a newly constructed object. Neither return changes the caller's variable until the caller assigns the result. - To predict a change, draw the caller and parameter as separate reference variables pointing to objects. Update the object for a mutator call, and update only the parameter arrow for parameter reassignment.
Passing an object reference copies its value into the parameter, so the method can change the caller's object; passing a primitive copies the value, so it can't. A method can return a reference to an object. A reference to no object is null — calling an instance method through it throws a NullPointerException, so check first.
מה השתבש? רק צוות האתר רואה זאת.