Are object's privitive members on the heap or on the stack?

Take any OOPs language with referencing as basic structure like java and c#.

For fast execution, they also support primitive types like int and char. I think this is done by storing them on the stack. and object types on the heap.

so for this:

class B

class A
   int a;
   B b;

Is A.a is in stack or on heap ?


The basic answer is that all local variables are on the stack and everything else is on the heap. However, as of Java 7 the compiler will perform a technique known as Escape analysis that checks whether an object is used strictly within a method (and doesn't escape that method), and upon finding such an object, will allocate its storage on the stack. This behavior was introduced with Java 6, Update 14, but not activated by default.

This, as many other examples, shows you that Java Language Specification is one thing and implementations another. As long as an implementation behaves as defined by the JLS, it is legit.

  • Class objects, including method code and static fields: heap.
  • Objects, including instance fields: heap.
  • Local variables and calls to methods: stack

But for java6 there are situations when objects are created on stack.



Someone asked me if he can run private base function. I told him that of course it is not possible (apart from trick of relection). But what the hell is this: public class MyClass { public ...

We are Using SUN JSF 1.2, WebSphere 7.0 for our application, we are getting ViewExpiredException only during the load testing I have gone through the below link javax.faces.application....

I have a text file in json, and I want to replace NumberInt(x) with the number x. In the text file, there are records/data which is in json that has a field workYear: NumberInt(2010) as an example. ...

I have a table from which I want to fetch all data on behalf of a specific column and then want to save that data in form of JSON so that I can send it over API to the database for saving. I am ...