pass-by-reference? pass-by-value?
最近项目中做一个tableCellContentKeyword,遇到一个棘手的问题
public void method (Integer i){
i = new Integer(6); //想改变i的值但是没有setXX的API方法,但实际上没有变化
}
真是把SCJP都忘光了这等业余的错误都犯=。=。。哎 好好复习下,注意文章最后一行大字,记住!
Does Java Use Pass-By-Value Semantics?
If Java passes objects by passing the reference variable instead, does that mean Java
uses pass-by-reference for objects? Not exactly, although you’ll often hear and read
that it does. Java is actually pass-by-value for all variables running within a single
VM. Pass-by-value means pass-by-variable-value. And that means, pass-by-copy-ofthe-
variable! (There’s that word copy again!)
It makes no difference if you’re passing primitive or reference variables, you are
always passing a copy of the bits in the variable. So for a primitive variable, you’re
passing a copy of the bits representing the value. For example, if you pass an int
variable with the value of 3, you’re passing a copy of the bits representing 3. The
called method then gets its own copy of the value, to do with it what it likes.
And if you’re passing an object reference variable, you’re passing a copy of the
bits representing the reference to an object. The called method then gets its own
copy of the reference variable, to do with it what it likes. But because two identical
reference variables refer to the exact same object, if the called method modifies the
object (by invoking setter methods, for example), the caller will see that the object
the caller’s original variable refers to has also been changed. In the next section,
we’ll look at how the picture changes when we’re talking about primitives.
The bottom line on pass-by-value: the called method can’t change the caller’s
variable, although for object reference variables, the called method can change the
object the variable referred to. What’s the difference between changing the variable
and changing the object? For object references, it means the called method can’t
reassign the caller’s original reference variable and make it refer to a different object,
or null. For example, in the following code fragment,
void bar() {
Foo f = new Foo();
doStuff(f);
}
void doStuff(Foo g) {
g.setName(“Boo”);
g = new Foo();
}
reassigning g does not reassign f! At the end of the bar() method, two Foo objects
have been created, one referenced by the local variable f and one referenced by
the local (argument) variable g. Because the doStuff() method has a copy of the
reference variable, it has a way to get to the original Foo object, for instance to call
the setName() method. But, the doStuff() method does not have a way to get to
the f reference variable. So doStuff() can change values within the object f refers
to, but doStuff() can’t change the actual contents (bit pattern) of f. In other
words,
doStuff() can change the state of the object that f refers to, but it can’t
make f refer to a different object!