Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DIsplaying Heap & String Literal Pool

 
Jean Johnson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic