public void test() { int a = 1; String tobePass = "before"; stu(a); Log.v(TAG, "after stu, a is: " + a); stu2(tobePass); Log.v(TAG, "after stu, tobePass is: " + tobePass); BeCopy beCopy = new BeCopy(); beCopy.flg = 10; stu3(beCopy); Log.v(TAG, "after stu, becopy.flg:= " + beCopy.flg); } private void stu(int a){ a = 2; } private void stu2(String tobe){ tobe = "after"; } private void stu3(BeCopy beCopy){ beCopy.flg = 100; } private class BeCopy{ int flg = 1; }
执行结果:
1,before, 100
总结:如果传递的是基础类型(包括string),java是传递副本。如果传递是对象,java是传递是引用。