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
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.
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.
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>