java中方法参数传递方式是按值传递。
如果参数是基本类型,传递的是基本类型的字面量值的拷贝。
如果参数是引用类型,传递的是该参量所引用的对象在堆中地址值的拷贝。
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public class {
static class MyObj{ int num;
public MyObj(int num) { this.num = num; }
public int getNum() { return num; }
public void setNum(int num) { this.num = num; } } public static void main(String[] args) { ReferencePkValue2 t = new ReferencePkValue2(); int a=99; t.test1(a); System.out.println(a);
MyObj obj = new MyObj(111); t.test2(obj); System.out.println(obj.num); }
public void test1(int a){ a=a+1; System.out.println(a); }
public void test2(MyObj obj){ obj.num=100; System.out.println(obj.num); } }
|
运行结果
近期评论