aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes stack and heap variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "stack and heap variable" Watch "stack and heap variable" New topic
Author

stack and heap variable

camelia codarcea
Ranch Hand

Joined: May 08, 2007
Posts: 36
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(){

}


SCJP 5.0 .... 97%
camelia codarcea
Ranch Hand

Joined: May 08, 2007
Posts: 36
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
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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,


cmbhatt
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
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

Joined: May 08, 2007
Posts: 36
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
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638


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 ]

    apigee, a better way to API!
    debasmita pattnayak
    Ranch Hand

    Joined: Apr 12, 2007
    Posts: 94
    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


    debasmita
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: stack and heap variable