java 当整数值是更改数组值之类的参数时,如何更改它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26185527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:28:01  来源:igfitidea点击:

How can I change Integer value when it is an argument like change array's value?

javaintegerpass-by-referencepass-by-value

提问by cow12331

public static void main(String[] args) {
    Integer i = new Integer(0);
    int[] arr = {1};
    p1(i);
    p2(arr);
    System.out.println(i);
    System.out.println(arr[0]);
}


public static void p1(Integer i) {
    i = 2;
}

public static void p2(int[] i) {
    i[0] = 2;
}

//output: 0, 2

//输出:0, 2

How can I change the value of i like I change the value of arr?

如何更改 i 的值,就像更改 arr 的值一样?

回答by Jon Skeet

You can't change the value of the variable iin mainfrom within the p1method, because the argument is passed by value: the parameter iin p1is entirely separate from the ivariable, it's just that they have the same value at the start of the method. Java alwaysuses pass-by-value semantics - but when the parameter type is a class, it's a referencethat is passed by value.

你不能在方法内部改变变量iin的值,因为参数是按值传递的:参数in与变量完全分开,只是它们在方法开始时具有相同的值。Java总是使用按值传递语义——但是当参数类型是一个类时,它是一个按值传递的引用mainp1ip1i

In fact, you're not changing the value of arr, either - it's a reference to the same array as before, but the value inthe array has been changed. And that's what you can't do with Integer, because Integeris an immutable type.

事实上,你不改变的价值arr,无论是-这是同一个数组作为前一个参考,但该值数组已经改变。这就是你不能做的事情Integer,因为Integer是不可变的类型。

If you want a mutable class like Integer, you could use AtomicIntegerinstead:

如果你想要一个像 一样的可变类Integer,你可以使用AtomicInteger

public static void main(String[] args) {
    AtomicInteger i = new AtomicInteger(0);
    modify(i);
    System.out.println(i);
}

private static void modify(AtomicInteger x) {
    x.set(2);
}

I would usually notdo this, however - I usually try not to modify the objects that method parameters refer to. Instead, I write methods which compute a single result, and return that.

但是,我通常会这样做 - 我通常会尽量不修改方法参数引用的对象。相反,我编写了计算单个结果并返回该结果的方法。

回答by Smutje

Simply put: You can't, because Integeris immutable and you only get the object address by value, so swapping the whole object is not possible because after the method finished, the old object gets reassigned.

简单地说:你不能,因为它Integer是不可变的,你只能通过值获取对象地址,所以交换整个对象是不可能的,因为在方法完成后,旧对象被重新分配。

回答by Dmitry Ginzburg

You can use AtomicInteger, which allows changing, instead of Integer:

您可以使用AtomicInteger,它允许更改,而不是Integer

public static void main(String[] args) {
    AtomicInteger i = new AtomicInteger(0);
    p1(i);
    System.out.println(i);
}


public static void p1(AtomicInteger i) {
    i.set(2);
}

回答by walkeros

Only by using some "hacks". You can do this like this:

只有通过使用一些“黑客”。你可以这样做:

public static void p1(Integer curInt) {
          Field field = curInt.getClass().getDeclaredField("value"); // Integer stores the real value in private field "value"
          field.setAccessible(true);
          field.set(curInt, 2);

}