通过值传递(pass by value)
当调用带参数的方法时,实参的值传递给形参,这个过程称为通过值传递。但是如果实参是变量而不是直接量,则将该变量的值传递给形参。无论形参在方法中是否改变,该变量不会受到影响。
1 public class Increment{
2 public static void main(String[] args){
3 int x=1;
4 System.out.println("Before the call,x is"+x);
5 increment(x);
6 System.out.println("After the call,x is"+x);
7 }
8 public static void increment(int n){
9 n++;
10 System.out.println("n inside the method is"+n);
11 }
12 }
结果如上
因为形参n和实参x有各自独立的存储空间,所以n值得改变并不会影响x的变化。
具体流程图如下: