我目前正在研究原子类,我无法跟踪它是“按值调用”还是“按引用调用”。我知道Java不允许按引用调用,它只是按值调用。但对于原子类,它似乎是通过引用调用的。你能和我分享一下你的想法吗?
最佳答案
它被称为“按值/引用传递”,它涉及将参数传递给方法。
原子类也不异常(exception),按值传递:
public void method1() {
AtomicInteger atomic = new AtomicInteger(5);
method2(atomic);
System.out.println(atomic.get()); // prints 5
}
public void method2(AtomicInteger atomic) {
atomic = new AtomicInteger(7); // reassign, but this does not affect metod1
}
关于java - 对于原子类,Java 如何按值管理调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6794075/