public class  CallDemo {
public static void main(String[] args) {
int x1 = 3;
method1(x1);
System.out.println(x1);int[] x2 = {3, 2, 4};
method2(x2);
for(int i = 0; i < x2.length; i++) {
System.out.print(x2[i] + ",");
}
}static void method1(int x) {
x = 0;
}static void method2(int[] x) {
x[0] = 0;
}
} //输出结果如下://3
//0,2,4,//为什么不是
//3
//3,2,4,
//或
//0
//3/2/4

解决方案 »

  1.   

    Pass by value
    This brings up the terminology issue, which always seems good for an argument. The term is “pass by value,” and the meaning depends on how you perceive the operation of the program. The general meaning is that you get a local copy of whatever you’re passing, but the real question is how you think about what you’re passing. When it comes to the meaning of “pass by value,” there are two fairly distinct camps: Feedback
    Java passes everything by value. When you’re passing primitives into a method, you get a distinct copy of the primitive. When you’re passing a reference into a method, you get a copy of the reference. Ergo, everything is pass by value. Of course, the assumption is that you’re always thinking (and caring) that references are being passed, but it seems like the Java design has gone a long way toward allowing you to ignore (most of the time) that you’re working with a reference. That is, it seems to allow you to think of the reference as “the object,” since it implicitly dereferences it whenever you make a method call. Feedback 
    Java passes primitives by value (no argument there), but objects are passed by reference. This is the world view that the reference is an alias for the object, so you don’t think about passing references, but instead say “I’m passing the object.” Since you don’t get a local copy of the object when you pass it into a method, objects are clearly not passed by value. There appears to be some support for this view within Sun, since at one time, one of the “reserved but not implemented” keywords was byvalue (This will probably never be implemented). Feedback
    Having given both camps a good airing, and after saying “It depends on how you think of a reference,” I will attempt to sidestep the issue. In the end, it isn’t that important—what is important is that you understand that passing a reference allows the caller’s object to be changed unexpectedly. Feedback
      

  2.   

    lz已经明白了,你还说那么多,明显看不起lz么