这取决于您输入的确切含义,特别是您是否指在调用站点作为参数出现的变量,例如,x in:
Object x = ...;
someMethod( x );
或者您正在谈论被调用函数将看到的实际对象,例如,实际的 Object 实例:
someMethod( new Object() );
变量的值(即它们引用的对象)不会改变,但你仍然可以对你得到的对象做一些事情。例如,
void appendX( StringBuilder sb ) {
sb.append('x');
}
void foo() {
StringBuilder builder = ...
appendX( builder );
builder.toString(); // will contain 'x' because you worked with the object that
// is the value of the variable `builder`. When appendX was
// was invoked, the value of its variable `sb` was the same
// object that is the value of foo's `builder`. Without
// changing what value the variable `builder` refers to, we
// did "change" the object (i.e., we appended 'x').
}
但是,更新方法中的引用不会更改方法之外的任何引用。在方法内部,您不能通过分配给方法的一个参数来更改方法外部的变量所引用的对象。例如:
void setNull( StringBuilder sb ) {
sb = null;
}
void foo() {
StringBuilder builder = ...
appendX( builder );
builder == null; // false, because the value of the variable `builder` is still the
// same object that it was before. Setting variable `sb` in setNull
// to null doesn't affect the variable `builder`. The variables are
// just a name so that you can refer to an value. The only way to
// what value a variable refers to is with an assignment.
}