STACK AND HEAP IN JAVA

Derek Honeybun

Total Post:17

Points:119
Posted by  Derek Honeybun
Java 
 920  View(s)
Ratings:
Rate this:
what is difference between stack and heap in Java . Can we create an object in stack memory? 
Does object of private class is also stored in heap memory??
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: stack and heap in java

    The stack is where local variables are stored. Each time you call a method, all the variables used by that method (including method parameters) get put on top of the stack. When a method returns, those variables are removed from the top of the stack. That way, the top of the stack always contains the variables for the method invocation currently running. Since variables only store primitives and object references, the actual objects need another place to stay. This is the heap. The heap is pretty much just a big piece of memory where objects and their fields are stored. 
    We cannot create objects on the stack. Objects are always created on the heap. 
    Whenever a method is invoked a "Stack frame" is given to the method,where method stores its local variable along with its parameter and it also stores there any information which might be required by a particular jvm. to execute it.Heap is a place where object gets memory as a buffer in which instance variable field of that particular class type is created 

    We cannnot mark a class private but we can mark its constructor private. Inner classes can be private, protected and static too.

      Modified On Jul-13-2015 06:53:26 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!