aspose file tools*
The moose likes Beginning Java and the fly likes Primitive variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Primitive variable" Watch "Primitive variable" New topic
Author

Primitive variable

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374

In this case refA is on stack, while object ClassA is on heap. Variable i is on stack, but does its value 10 is on heap or stack ? If its on stack, then it means variable and its value both are on stack for primitive, right ? What I mean to ask is, in case of object, instance reference variable is on stack, but object is on the heap. Likewise, in case of primitive, primitive reference is on stack, but des it value go on stack or heap (which is 10 for i and 20 for j in this case).
I went through various threads, but could not find clear answer to this.

Can someone please answer this without pointing to different thread ?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
No, we can't answer that because we don't know whether those are fields or local variables.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Now you have changed the code, so we all know i and refA are fields and j and t1 are local variables, where do you think the values associated live, and where the pointers are stored.
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
refA and t1 are reference variables, so they will be on stack.
i and j are primitive variables so they will also be on stack but the their values of 10 and 20 will be on heap.
May be i will also be on heap, but j will be on stack only, but its value 20 eill be on heap.

Is this right ?

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Only fields (be it primitive or Object references) local to that method live on the Stack. In case of the instance- the memory for this is allocated on the heap and the reference in the stack points to the memory allocated on heap. Each method call will push a StackFrame to the Stack. The Stack frame will have the variables and values for those variables. Values for primitives would be 10,20 and so on and values for references would be the address on the heap for the instance created.

The Instance variables- Those defined in the class/ those part of the instance will be part of the memory allocated for that instance on the heap.


Mohamed Sanaulla | My Blog
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374


When this code is run, main() is created on stack, reference variable "o" is created on the stack in main(), while instance ObParm is created on heap.
I am confused with the execution of fields and methods of instance created. Can someone let me know, how does theexecution of above code takes place on heap and stack ? I understand it following way, but again I am confused in that.
Where does amethod() get called is it on heap or stack ? amethod has local variable i, which stays on heap and has a reference variable v, which creates another instance on heap. amethod() calls another(). Where does another() get called on heap or stack ?
Please explain step by step execution of this code. I dont understand execution of this code.

Thanks
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Can you first explain to us, in your own words, what the stack is, and what the heap is?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

And if you have the book: Inside the Java Virtual Machine by Bill Venners- I would recommend you to read the 5th chapter of that book- Explained the execution of a Java program in required depth.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Primitive variable