File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes DIsplaying Heap & String Literal Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "DIsplaying Heap & String Literal Pool" Watch "DIsplaying Heap & String Literal Pool" New topic
Author

DIsplaying Heap & String Literal Pool

Jean Johnson
Greenhorn

Joined: May 13, 2003
Posts: 1
Hello,
How do I display the heap and the String Literal Pool? I'm trying to come up with some examples for the exam. I'm trying to write some examples so I can get a better understanding of when String Objects are created, when String literals are created and to be able to see the JVM at various positions in the code. Ideally, I'd like to be able to print the state of the JVM's heap, stack and String Literal pool out.

Also is there any way to display the reference variable's bit pattern? I've programmed in C before and I used to be able to print out the memory address there but I can't find a way to do it Java. I want to do this so I can show the reference variables pointing to the same memory location as I point more than one String reference variable to the same String literal.
Thanks in advance for your help.
Jean
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I could be wrong, but here is the reason why I think you can only see the literal string pool with a debugger written specifically for your virtual machine.
The API doc for String intern says �A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.�
Here is the source code for String.intern
public native String intern();
I conclude that if there is a literal string pool, the implementation depends on the virtual machine and the platform.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Jean Johnson:
Also is there any way to display the reference variable's bit pattern? I've programmed in C before and I used to be able to print out the memory address there but I can't find a way to do it Java. I want to do this so I can show the reference variables pointing to the same memory location as I point more than one String reference variable to the same String literal.

There is no direct way to do that in Java. To prove that two Strings have the same memory location simply compare them using ==. In Java, since there is no operator overloading, the == always does an address comparison for objects.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I am also interested in knowing more about the literal string pool. I spent part of yesterday searching the Sun site and found nothing.
Here is the best source of information that I know of.
Inside the Virtual Machine by Bill Venners
http://www.artima.com/insidejvm/ed2/index.html
Chapter 8 The Linking Model
http://www.artima.com/insidejvm/ed2/linkmodP.html
Chapter 8 has information about string literals.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Jean,
If you are interested in the behavior of the String.intern method and the use of the equality operator with String references, then you might want to take a look at the String exam in section 8 of my Single Topic exam set. Some have complained that I covered that topic ad nauseam.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DIsplaying Heap & String Literal Pool