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
java Integer reference
提问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 Integer
that derive from Java's abstract Number
class 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 < 127
instead 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 int
argument, 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