哪个对象是在内存的哪一部分创建的?
public class Order
{
static Customer cust = new Customer();
string sEmpty = "";
public static void main(String args[])
{
int iTotal = 10;
string sProductName = "Salt";
Ship shp = new Ship();
}
}
在上面的代码中,哪个对象和引用是在内存的哪一部分创建的? (我的意思是堆和堆栈)
(来源:c-sharpcorner.com)
public class Order
{
static Customer cust = new Customer();
string sEmpty = "";
public static void main(String args[])
{
int iTotal = 10;
string sProductName = "Salt";
Ship shp = new Ship();
}
}
At the above code, which object and reference is created in the which part of memory? (I mean Heap and Stack)
(source: c-sharpcorner.com)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您将问题标记为 Java,所以我假设您的意思是 Java。直接从马嘴里说出来:
JVM 规范
这里是 链接到之前的一个SO问题,该问题详细讨论了这个问题(并且是关于该主题的与语言无关的讨论)。
下面是 C# 角的一篇文章的链接,详细介绍了该问题在 C# 中。
Since you tagged your question Java, I'll assume you meant in Java. Straight from the horse's mouth:
JVM Spec
Here is a link to a previous SO question that goes into this in serious detail (and is a language-agnostic discussion on the topic).
Here's a link to an article from C# corner detailing the issue in C#.
Order 和 Customer 都在堆上。尽管Customer可能是一个结构体,但它是引用类型(例如,类)的组合成员。
所有字符串都是引用类型并在堆上创建。
我不确定 Ship 类,因为我没有它的声明(即,我不知道它是结构还是类)。
int iTotal 变量在堆栈上创建。
对于 C# 来说也是如此。 Java 可能有不同的规则。
Order and Customer are on the heap. Though Customer may be a struct, it is a composed member of a reference type (e.g., a class).
All strings are reference types and are created on the heap.
I'm not sure about the Ship class because I don't have its declaration (i.e., I don't know if it is a struct or a class).
The int iTotal variable is created on the stack.
This is true for C#. Java may have different rules at play.