• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Primitive variable

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, we can't answer that because we don't know whether those are fields or local variables.
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you first explain to us, in your own words, what the stack is, and what the heap is?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic