My understanding has always been, regardless of C++ or C# or Java, that when we use the new
keyword to create an object it allocates memory on the heap. I thou
In Java and C#, we don't need to allocate primitive types on the heap. They can be allocated on the stack ( not that they are restricted to stack ). Whereas, in C++ we can have primitive as well as user defined types to be allocated on both stack and heap.
Java 7 does escape analysis to determine if an object can be allocated on the stack, according to http://download.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html.
However, you cannot instruct the runtime to allocate an object on heap or on stack. It's done automatically.
In c#, a class
always lives on the heap. A struct
can be either on the heap or stack: