@w1024020103
2017-02-05T22:00:23.000000Z
字数 1022
阅读 726
cs61b
public class PassByValueFigure {
public static void main(String[] args) {
Walrus walrus = new Walrus(3500, 10.5);
int x = 9;
doStuff(walrus, x);
System.out.println(walrus);
System.out.println(x);
}
public static void doStuff(Walrus W, int x) {
W.weight = W.weight - 100;
x = x - 5;
}
}
Does the call to doStuff have an effect on walrus and/or x? Hint: We only need to know the GRoE to solve this problem.
Answer: 3400,10.5 9
I thought if there's a return value of doStuff that is int type, x would change. let's try.
But still i got x = 9!!
I didn't get the point thoroughly i guess.
My understanding is that according to the visualizer shows,doStuff method and main method take different place in the memory. As for reference variables like walrus in two method, they actually point to the same object new Walrus, so they are the same thing. However, as far as the primitive type int x is considered, things are different. Because int x only take one place which is different for these two method in memory, and they don't point to object never mention the same object. That's why the two X s are not the same thing.