Results 1 to 4 of 4

Thread: Heap or Stack

  1. #1
    Metaferia is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Heap or Stack

    Hi,

    I would like to know if the following are stored in the heap or stack memory? (using the eclipse Java)

    Java Code:
    String s;
    s ?

    Java Code:
    s = hello.changeString("Test")
    s ?

    Java Code:
    Int n;
    n?

    Java Code:
    public int goalInt(int nNew){
    b = nNEW;
    return nNEW + 2
    }
    nNEW ?


    Thanks

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by Metaferia View Post
    I would like to know if the following are stored in the heap or stack memory? (using the eclipse Java)

    First of all, Eclipse has nothing to do with heap or stack storage.

    Then, to keep it very simple, your objects live on the heap and you methods live on stack.

    The variables which you declare inside you class are nothing but the instance variables. Your instance variables live inside the objects created, means on the heap.

    The variables declared inside the methods are local variables. And your local variables live inside the methods [as they live as long as the method is alive] which means on the stack.

    However, the object references live depending on where they have been declared. Object reference declared as instance variable lives inside the class object on the heap, whereas, the object reference declared inside the method lives on the stack along with method. No matter where the object references are declared, the object always live on the heap.

    The method parameters are also considered as local variables and so, they live on stack as well.

    Now can you figure out on your own which thing in your code lives where?

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by Metaferia View Post
    Java Code:
    Int n;
    There is nothing like, "Int" in java. Either use the wrapper class properly like,
    Java Code:
    Integer n;

    Or use the primitive data type like,

    Java Code:
    int n;

    Java is case sensitive, be careful.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    Metaferia is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Increasing heap/stack size (MacOSX)
    By CapsLoc in forum New To Java
    Replies: 3
    Last Post: 12-13-2010, 10:52 AM
  2. The Stack and the Heap
    By al_Marshy_1981 in forum New To Java
    Replies: 9
    Last Post: 06-25-2010, 09:45 AM
  3. no heap space... need more heap
    By anupam.kumar in forum Advanced Java
    Replies: 3
    Last Post: 02-08-2010, 04:42 PM
  4. Stack implementation
    By jefrazie in forum New To Java
    Replies: 3
    Last Post: 10-07-2009, 12:59 PM
  5. Help with heap and stack
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 02:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •