| English | 释义 | 拼音 |
|---|---|---|
| NullPointerException/nʌl ˈpɔɪntə ekˈsepʃn/ | 空指针异常 | kōng zhǐ zhēn yì cháng |
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.
复制了引用(共享对象);复制了基本数据类型
对象的 score 改变;基本类型 x 不变。
当你把一个对象传给方法,方法收到的是……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Java 将引用值复制到独立的形参变量中。两个引用可指向同一对象;对形参重新赋值不会替换调用者的变量。
方法可以通过修改它的形参来改变调用者的 int 变量。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
基本类型被复制——调用者的 int 不受影响。
通过 null 引用调用实例方法会抛出...
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
null 指向没有对象,所以没有东西可以在其上调用方法。
方法可以返回一个指向对象的引用,调用者随后共享它。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
一个方法可返回现有的对象引用并与调用者共享其对象。另一个方法可构造并返回一个新对象。
要安全地使用一个可能为 null 的引用,你应该先检查……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
在其上调用方法前先防 null。
每个返回 学生 引用的方法都必须返回一个先前存在的对象。
难度 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"); 但调用者忽略了该结果。调用者的现有 学生 变量会发生什么变化?
难度 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.