aspose file tools*
The moose likes Java in General and the fly likes can explain about heap and pool variables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "can explain about heap and pool variables?" Watch "can explain about heap and pool variables?" New topic
Author

can explain about heap and pool variables?

Mallik Avula
Ranch Hand

Joined: Nov 30, 2006
Posts: 86
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


Thanks & Regards<br />Mallik Avula<br />SCJP1.4
Akshayan Venkatesh
Greenhorn

Joined: Apr 13, 2006
Posts: 26
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

Joined: Jun 04, 2007
Posts: 320
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.


------------------------
Bob
SCJP - 86% - June 11, 2009
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

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 ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

Joined: Mar 22, 2007
Posts: 329

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
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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...


"I'm not back." - Bill Harding, Twister
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

Joined: Jun 04, 2007
Posts: 377
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can explain about heap and pool variables?