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)
订单和客户都在堆。虽然Customer可以是结构,但它是引用类型(例如,类)的组合成员。
所有字符串都是引用类型,并在堆上创建。
我不确定Ship类,因为我没有声明(即,我不知道它是结构还是类)。
int iTotal变量在堆栈上创建。
这对C#来说是正确的。 Java可能有不同的规则在起作用。