了解 BigDecimal class 算术和对象行为

To understand BigDecimal class Arithmetic and Object Behaviour

public final class Test{

    public static void main(String args[]) throws Exception {

        BigDecimal bigDecimal_One = BigDecimal.ONE;
        System.out.println(bigDecimal_One);

        bigDecimal_One.add(BigDecimal.ONE);// 1. As I have not set bigDecimal_One = bigDecimal_One.add(BigDecimal.ONE) hence variable Value would be unchanged 
        System.out.println(bigDecimal_One);

        addTenTo(bigDecimal_One);// 2. I wanted to know why still value is unchanged despite reference pointing to new value
        System.out.println(bigDecimal_One);
    }

    public static void addTenTo(BigDecimal value) {
        value = value.add(BigDecimal.TEN);
        System.out.println("Inside addTenTo value = "+value);
    }
}

当我 运行 这个程序时,我得到低于输出

1
Inside addTenTo value = 11
1

有人可以解释一下为什么即使在方法 addTenTo(即 11)中已将引用分配给新值,变量 bigDecimal_One 的值仍然不变吗?

我的答案是内嵌的...

public static void addTenTo(BigDecimal value) {
    value = value.add(BigDecimal.TEN); // `value` here is a local variable, which does not affect the parameter-`value` 
    System.out.println("Inside addTenTo value = "+value);
}