假设我们有以下代码:
void method() {
int[] test = new int[3];
test[0] = 0;
test[1] = 1;
test[2] = 2;
}
根据我在Jon Skeet的SO文章中所读到的内容,该new int[3]
部分等效于:
public class ArrayInt3 {
public readonly int length = 3;
public int value0;
public int value1;
public int value2;
}
那么,这是否意味着test
(对的引用ArrayInt3
)在堆栈上?那是ArrayInt3
不是就在堆上?而且我想value0
,value1
和value2
也都在堆上(例如,在此示例中为0、1、2)?
因此,总共有4个对象在堆上,对吗?
堆栈上将没有任何对象。在堆栈上,将有一个对int[]
堆中存储的单个对象的引用值。
您必须开始区分对象,变量和值。
局部变量是堆栈中方法stackframe的一部分。因此,的参考值int[]
将存储在堆栈中的变量中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句