Java 内存、按值传递、按引用传递
可能的重复:
Java 是按引用传递吗?
我有关于按值传递和按引用传递的问题在Java中。
我编写了一个“Graph”类,用于将长双精度数组显示为图形,(简化的)构造函数如下所示。
private double[] Values;
public Graph(double[] values) {
Values = values;
}
该数组可能很长并且占用合理的内存量。
本质上我的问题是这样的:如果我调用构造函数来创建一个新图,数组“Values”将是传递给它的数组的副本,还是一个引用?
在我看来,基元是“按值传递”,而对象是“按引用传递”,这应该意味着数组将是一个副本。尽管我知道这个定义在技术上并不正确。
如果我是正确的,并且该数组是一个副本,那么减少该类使用的内存量并引用另一个类的数组的最佳方法是什么? 抽象 GetValues() 方法是实现此目的的好方法吗?
预先感谢,
克里斯。
Possible Duplicate:
Is Java pass by reference?
I have a question about passing by value and passing by reference in java.
I have a "Graph" class that I wrote to display a long array of doubles as a graph, The (simplified) constructor looks like this.
private double[] Values;
public Graph(double[] values) {
Values = values;
}
The array can be quite long and take up a reasonable amount of memory.
Essentially my question is this: If I call the constructor to create a new graph, will the array "Values" be a copy of the array that's passed to it, or will it be a reference?
In my mind, Primitives are "pass by value" and Objects are "pass by reference", which should mean that the array would be a copy. Although I'm aware that this definition is not technically correct.
If I am correct, and the array is a copy, what would be the best way to reduce the amount of memory this class uses, and reference the array from another class?
Would an abstract GetValues() method be a good way of achieving this?
Thanks in advance,
Chris.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
虽然
double
是原始类型,但double[]
是对象类型(数组),因此,不,整个数组不会传递给构造函数,而是数组将作为“参考值”传递。您将无法替换构造函数内的数组,但如果需要,您可以替换数组中的各个值。While
double
is a primitive type,double[]
is an Object type (array), so, no, the entire array will not be passed to the constructor, instead the array will be passed as "value of a reference". You will not be able to replace the array inside the constructor, but you could, if you wanted, replace individual values in the array.Java 是按值传递的。
请参阅 JLS,4.12.3 变量类型:
编辑:澄清我的答案:Java 的类型分为两类:基元类型和引用类型。每当您调用方法(或构造函数)时,参数都会被复制(因为 Java 是按值传递的)。基元被完全复制,对于引用类型,引用被复制。 Java 永远不会自动深度复制任何内容,因此由于数组是引用类型,因此只会复制对数组的引用。
Java is pass-by-value, period.
See the JLS, 4.12.3 Kinds of Variables:
EDIT: To clarify my answer: The types of Java are divided in two categories: The primitives and the reference types. Whenever you call a method (or a constructor), the parameters get copied (because Java is pass-by-value). The primitives get copied entirely and for reference types, the reference gets copied. Java will never automatically deep copy anything, so as arrays are reference types, only the reference to the array gets copied.
它将是
值
的引用。 Java是按值传递的,但按值传递的是对数组的引用,因为数组是一个对象。另请参阅几天前的此答案前。
It will be a reference of
values
. Java is Pass-by-value, but what's passed by value is a reference to the array, as the array is an object.See also this answer, from just a few days ago.
这将是一个参考。参数
values
通过“按值引用”传递,并且该引用附加到Values
。因此 - 对 Graph.Value 的任何更改也将反映到值,反之亦然。
It will be a reference. the parameter
values
is passed "reference by value", and the reference is attached toValues
.Thus - any cahnge to
Graph.Value
will also be reflected tovalues
and vise versa.数组是一种引用类型,通过副本传递仅适用于原始类型,而数组则不然。顺便说一下,其他引用类型包括类和接口。
Array is a reference type, passing by copy applies only to primitive types, which an array isn't. The other reference types include classes and interfaces, by the way.