Which object is created in which part of memory?
Question
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)
Solution
Since you tagged your question Java, I'll assume you meant in Java. Straight from the horse's mouth:
The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.
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#.
OTHER TIPS
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.