java java整数引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6562337/
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-10-30 16:22:46  来源:igfitidea点击:

java Integer reference

java

提问by OneMoreVladimir

I've got a question.

我有一个问题。

public class Jaba {

    public static void main(String args[]) {
        Integer i = new Integer(0);        
        new A(i);
        System.out.println(i);
        new B(i);
        System.out.println(i);
        int ii = 0;        
        new A(ii);
        System.out.println(ii);
        new B(ii);
        System.out.println(ii);    
    }

}

class A {

    public A(Integer i) { ++i; }

}

class B {

    public B(int i) { ++i; }

}

To my mind passing an int\Integer as Integer to a function and making ++ on that reference should change the underlying object, but the output is 0 in all the cases. Why is that?

在我看来,将 int\Integer 作为 Integer 传递给函数并在该引用上创建 ++ 应该会更改底层对象,但在所有情况下输出均为 0。这是为什么?

采纳答案by Pa?lo Ebermann

As said in the other answers, Java does only call-by-value, and the ++operator only effects a variable, not an object. If you want to simulate call-by-reference, you would need to pass a mutable object, like an array, and modify its elements.

正如其他答案中所说,Java 只执行call-by-value,并且++运算符只影响变量,而不是对象。如果要模拟call-by-reference,则需要传递可变对象(如数组)并修改其元素。

The Java API has some specialized objects for this, like java.util.concurrent.atomic.AtomicInteger(which additionally also works over multiple threads), and org.omg.CORBA.IntHolder(used for call-by-reference for remote calls by the CORBA mechanism).

Java API 有一些专门的对象,例如java.util.concurrent.atomic.AtomicInteger(它还可以在多个线程上工作)和org.omg.CORBA.IntHolder(用于通过 CORBA 机制进行远程调用的按引用调用)。

But you can also simply define your own mutable integer:

但您也可以简单地定义自己的可变整数:

class MutableInteger {
    public int value;
}


class C {
   public C(int[] i) {
       ++i[0];
   }
}
class D {
   public D(MutableInteger i) {
       ++i.value;
   }
}
class E {
   public E(AtomicInteger i) {
       i.incrementAndGet();
   }
}
public class Jaba {
    public static void main(String args[]) {

        int[] iii = new int[]{ 0 };
        System.out.println(iii[0]);
        new C(iii);
        System.out.println(iii[0]);
        MutableInteger mi = new MutableInteger();
        System.out.println(mi.value);
        new D(mi);
        System.out.println(mi.value);
        MutableInteger ai = new AtomicInteger(0);
        System.out.println(ai);
        new E(ai);
        System.out.println(ai);
    }
}

回答by Alnitak

Most of the classes such as Integerthat derive from Java's abstract Numberclass are immutable., i.e. once constructed, they can only ever contain that particular number.

大多数类(例如Integer从 Java 的抽象Number类派生的类)都是不可变的。即,一旦构造完成,它们就只能包含该特定数字。

A useful benefit of this is that it permits caching. If you call:

这样做的一个有用的好处是它允许缓存。如果你打电话:

Integer i = Integer.valueOf(n);

for -128 <= n < 127instead of:

用于-128 <= n < 127代替:

Integer i = Integer.new(n)

you get back a cached object, rather than a new object. This saves memory and increases performance.

你会得到一个缓存的对象,而不是一个新的对象。这可以节省内存并提高性能。

In the latter test case with a bare intargument, all you're seeing is how Java's variables are passed by valuerather than by reference.

在后一个带有裸int参数的测试用例中,您所看到的只是 Java 的变量是如何按值而不是按引用传递

回答by Jarek Potiuk

@Alnitak -> correct. And to add what really happens here. The ++i due to autoboxing works like that:

@Alnitak -> 正确。并添加这里真正发生的事情。由于自动装箱而产生的 ++i 是这样工作的:

int val = Integer.intValue(); ++val;

and val is not stored anywhere, thus increment is lost.

并且 val 没有存储在任何地方,因此增量丢失。

回答by gun9

If you want to use reference parameter then try this.

如果你想使用引用参数,那么试试这个。

IntHolder http://docs.oracle.com/javase/7/docs/api/org/omg/CORBA/IntHolder.html

IntHolder http://docs.oracle.com/javase/7/docs/api/org/omg/CORBA/IntHolder.html