This week's book giveaway is in the Python forum.
We're giving away four copies of Hello World and have Warren & Carter Sande on-line!
See this thread for details.
Win a copy of Hello World this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Heaps & Objects

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read Objects are always on Heap,whether its local reference or not.
There is nohing like stack object.All the instance members reside on heap ithin object.
but in K &B book ,chap3 page 271 Q10 says something different.

Some Referene Variables live on stack ,some on heap.
Explanation in book:when an object has a reference variable ,the reference variable lives on stack .

Which is in contradiction to what i read.
Is this true,references inside object ..resides on stack.

THank you
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Lucky..
Consider this code::



here the reference variable 'hs' resides in Heap while the local reference variable 'lhs' resides in
the stack of method heapStackObject(), but the object lhs referring to is
always created on Heap and this Object will have 'hs' as one of it's members(which is on that heap).

now a pop quiz..where do the static reference variable reside??in heap or stack??
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static variable are stored in Static Storage

Static storage. �Static� is used here in the sense of �in a fixed location� (although it�s also in RAM). Static storage contains data that is available for the entire time a program is running. You can use the static keyword to specify that a particular element of an object is static, but Java objects themselves are never placed in static storage

The Final variables are stored in Constant storage area.

Constant Storage:Constant values are often placed directly in the program code, which is safe since they can never change. Sometimes constants are cordoned off by themselves so that they can be optionally placed in read-only memory (ROM).
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More on the first question.
Object references can be placed both on the stack and on the heap depending on where they're declared. If they're declared in a method they will be placed on the stack (during method execution). If they're declared outside a method they will be placed on the heap (and stay there during the lifetime of the object).

The above picture is somewhat changed with version 6 of Java which intriduces an optimization technique called "escape analysis". It allows objects that only lives as long as the method where they're created to be allocated on the stack (insted of on the heap).

So to say that objects are always allocated on the heap will become wrong. Objects with a method-long lifespan can be allocated on the stack.
 
crispy bacon. crispy tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!