• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can explain about heap and pool variables?

 
Mallik Avula
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
i need explanation for the following doubts
where the memory allocate for premitives?
where for objects?
if i declare string as String str = "abc";, where it will go?
if i do as String str = new String("abc"); where it will go?

please give me explanation...

regards
Mallik
 
Akshayan Venkatesh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The memory allocated in this sense String Literal Pool area.

Object are normally stored in the memory place of HEAP

I also like to tell Object reference are stored in the memory place of STACK
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding might be a little weak but I thought that, a reference variable, str, would be allocated on the stack. Then a String object would be allocated on the heap and the reference to it would be placed in str.

In that manner, when the current method is completed and return made, the recovery from the stack will place str out of scope (thus, destroying it) and the String object left on the heap is elligible for GC.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bob Ruth:
My understanding might be a little weak but I thought that, a reference variable, str, would be allocated on the stack. Then a String object would be allocated on the heap and the reference to it would be placed in str.

In that manner, when the current method is completed and return made, the recovery from the stack will place str out of scope (thus, destroying it) and the String object left on the heap is elligible for GC.


That's right but don't forget:

- An instance variable that is a reference to another object (thru aggregation) lives in the heap, because it is part of an object.
- All objects (this includes their instance variables) live in the heap, this includes literal strings. The Literal String Pool is just a pool of references to string objects that were created during loading of a class that "declared" a string literal. But the String objects themselves live in the heap. The references in the String pool are used in order to avoid creating more than one objects that represent the same string literal.
[ June 16, 2007: Message edited by: Sergio Tridente ]
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sergio Tridente:

- An instance variable that is a reference to another object ...
- All objects (this includes their instance variables) ...

It seems that you have confused something, since you mention instance Variables twice with different behaviour.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manfred Klug:

It seems that you have confused something, since you mention instance Variables twice with different behaviour.


There's no different behaviour: I just said it twice to reinforce what I was trying to mean. Please correct me if I am wrong.
 
Henry Wong
author
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is great that the current SCJP tests goes into such detail, but please don't spend too much time memorizing this.

Java 6 added a new optimization technique that may cause certain object to be allocated on the stack. For those who are interested, google "escape analysis".

Henry
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Henry]: It is great that the current SCJP tests goes into such detail

As a matter of fact, I'm pretty sure it doesn't test on any of the topics brought up in the original post. Let's move this off to JiG - Intermediate where it will not cause so much worry for SCJP takers...
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
[b]As a matter of fact, I'm pretty sure it doesn't test on any of the topics brought up in the original post. Let's move this off to JiG - Intermediate where it will not cause so much worry for SCJP takers...


I don't know the questions in the exam , but I think that Kathy and Bert had a reason to mention the distinction between stack and heap in the two-minute drill and in the self-test.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sergio Tridente:
There's no different behaviour: I just said it twice to reinforce what I was trying to mean. Please correct me if I am wrong.


Sorry,

I don't say you are wrong. Your statement sounds, as if you thought of something special if an instance variable is a reference to another object.

If you only wanted to mention it twice, all is ok.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic