• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

stack and heap variable

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello
I am preparing for SCJP 5.0 and I have read a few chapters from K/B book, but I don't quite understand the isue about varibles stored on stack or on the heap: I thought I understood until the one question from the Chapter 3's test, where it sais that the following statement is true:
"Some reference variables live on the stack, and some live on the heap" (Q 10, option C)

In the book it sais that local var live on the stak and objects and instance var live on heap:
What happens in the following cases ?

class Test{
int number;
Animal a = new Animal();

public void method(){

}
 
camelia codarcea
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I didn't finish the message (press post by mistake)
so
public void method {
int newNumber = number + 1;
Object[] obj = new Object[3];
}

Could you tell me where will each of these variables be stored ?
Tnak you
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
camelia:


In the book it sais that local var live on the stak and objects and instance var live on heap:
What happens in the following cases ?

class Test{
int number;
Animal a = new Animal();

public void method(){

}



In this case if you create an object of class Test that will encapsulate
object of the Animal and a member variable number; So inside the Test
object, there will be number(primitive member) and Animal object on the
heap. The ref variable t1 will be on the stack referring to object test on
the heap;



Thanks,
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, that only "obj" will live on stack, obj[0],obj[1],obj[2] will be in heap.

My reasoning is that, if obj[0] lived on stack, then after method is finished it will go out of scope and cease to exist.



After this method ends, obj[0] is still accesible.

bytecode:


Transferring 100000 items would take more than 1 instruction.

<my best guess>
Everything creating via "new" should live on heap. (If no, please give some example :-))
</my best guess>
 
camelia codarcea
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello , and thanks for your answers

I see thinks more clearly now (at least I think so )
I think that John is right: obj[0], obj[1], obj[2] are on the heap, because when you use "new", you create something on the heap, and that something happens to be (in this case) three other references to Object objects.
So, could this also be the case that applies to the the sentence in the test? that some reference variable live on the stack and others on the heap ?
in my case:
obj is a var reference on the stack
obj[0], obj[1] and obj[2] are also reference variable and live on the heap ?

Thank you
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


obj[0], obj[1] and obj[2] are also reference variable and live on the heap ?


I doubt whether obj[0],[1], etc. are reference variables on the heap.
This is just a notation to access an element at a particular index in an array.
The only reference variable is obj here and that is a reference to the array object.
Arrays beings first class objects in java, they are stored on the heap.
The array object on the heap may have references to the memory location for inidividual elements(if not all elements are stored in contiguous memory location) but they are not obj[0], [1], etc.
FYI:
  • The reference variables that lie on the heap are member variable references.
  • Class level variable references(static) lie in the method area or more specifically the constants pool for that class.
  • All primitives lie either on the stack or in the constants pool but never on the heap.

  • [ May 09, 2007: Message edited by: Nitesh Kant ]
     
    Ranch Hand
    Posts: 94
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi,
    this is my explaination about the problem:

    when you are doing this:
    Object[] obj = new Object[3];
    you are creating only one reference of object type i.e. obj in this case.
    so obj will be going to the stack
    the actual objects, i mean when the obj is a intialised with values, those will be stored in the heap.

    hope this clarifies your doubt

    i completely agree with JOHN
    reply
      Bookmark Topic Watch Topic
    • New Topic