Passing and Returning References
| English | Chinese | Pinyin |
|---|---|---|
| NullPointerException | 空指针异常 | kōng zhǐ zhēn yì cháng |
Passing an object to a method
- When you pass an object to a method, you pass its reference (address), not a copy.
- 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.
- Objects and primitives behave differently here — a key distinction.
Primitives vs. objects as arguments
- A primitive argument is copied — the method can't change the caller's variable.
- An object argument shares the reference — the method can change the object's state.
changeInt(x)can't alterx;student.addScore(10)does change that student.- Ask: is the argument a value (copied) or an object (shared)?
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.- The returned reference points at the same object, not a copy.
- The caller can then call methods on the returned object.
The null reference
- A reference that points at no object is
null. - Calling a method on
nullthrows a NullPointerException 空指针异常. - Returning
null(e.g. "not found") is common — the caller must check for it. - Guard with
if (s != null) { s.addScore(10); }before using a possibly-null reference.
Passing an object shares its reference — the method can change the caller's object; passing a primitive copies the value — it can't. So list.addScore(10) mutates the caller's object, but changeInt(x) leaves the caller's x untouched. And calling a method on a null reference throws a NullPointerException — check != null first when a reference might be null.
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).- Same idea, opposite result — object shared, primitive copied.
Passing an object to a method shares its reference, 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 a method on it throws a NullPointerException, so check first.
Object shared, primitive copied
The object's score changes; the primitive x does not.
When you pass an object to a method, the method receives...
Objects share a reference, so the method can change the caller's object.
A method can change the caller's int variable by modifying its parameter.
Primitives are copied — the caller's int is untouched.
Calling a method on a null reference throws...
null points at no object, so there's nothing to call the method on.
A method can return a reference to an object, which the caller then shares.
The returned reference points at the same object.
To safely use a reference that might be null, you should first check...
Guard against null before calling a method on it.