• Post Reply Bookmark Topic Watch Topic
  • New Topic

stack and heap in java  RSS feed

 
abhijeet kush
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Whether classes are private or not does not influence how they are stored at all.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Call stack on Wikipedia to understand what the stack is and how it works.

You cannot create objects on the stack. Objects are always created on the heap*.

* Newer versions of Java have a special optimization, escape analysis, so it is strictly speaking no longer true that objects are always allocated on the heap. However, this is an advanced implementation detail that you don't need to know about. You still cannot explicitly allocate an object on the stack instead of on the heap.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"stack" and "heap" are overloaded terms. They are both DATA STRUCTURES as well as KINDS OF MEMORY. The way you word your post, we assume you are talking about the latter, but your subject line is unclear.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I want to add one more thing.. a class cannot be marked private. A class can only be marked public,abstract or final.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya but we can make its constructor private..
So problem solved
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:Also I want to add one more thing.. a class cannot be marked private. A class can only be marked public,abstract or final.


Inner classes can be private, protected and static too.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!