java方法间参数传递规则

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是传递是引用。

 

上一篇:autojs指纹验证


下一篇:WXSS——样式(6)