问题描述
我一直在阅读了很多,现在我真的很迷茫。考虑一个普通的实例:
I have been reading a lot and now i really confused. Consider an ordinary instantiation:
Sampleclass instance1 = new Sampleclass();
读了很多之后,我才知道,实例1
是存储在堆栈包含存储在堆中对象的数据的存储地址的引用变量。
After reading a lot I came to know that instance1
is a reference variable stored in a stack which contains the memory address of the object's data which is stored in heap.
如果这是正确的话,其中的对象吗? 实例1
也是一个对象。有时我看到像新Sampleclass只有声明()
。是否有足够的对象实例化?
If this is correct then where is object? instance1
is also a object. Sometimes I have seen only declaration like new Sampleclass()
. Is that sufficient for object instantiation?
推荐答案
实例1
是一个变量。
由于它的类型是引用类型,它是一个对生活在堆上的对象实例。
Because its type is a reference type, it is a reference to an object instance that lives on the heap.
新SampleClass()
是在堆上创建一个新的对象,并返回一个引用给它的构造函数调用。
new SampleClass()
is a constructor call that creates a new object on the heap and returns a reference to it.
这篇关于什么是引用变量和对象之间的实际差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!