Tuesday, July 31, 2007

Java reference types - an example and questions

Dr. Heinz Kabutz has an excellent example for Soft, Weak, and Phantom references in one of his newsletters. You can view it here.

I also created mind maps for the Java garbage collector and Java reference types. You can download the images by clicking on the links below:

  1. Java garbage collector
  2. Reference types in Java 


Before concluding the series on Reference types in Java, here are some simple questions for you to test your knowledge.

Q1. Consider the following code:

String s = new String("abc");
SoftReference sf = new SoftReference(s); 

What is the reference type (strong or soft) for "abc" ?

Q2. What is the main difference between phantom  references and the other (soft and weak) two references?

Q3. Consider the following code:

SoftReference sf = new SoftReference("abc"); 
WeakReference wr = new WeakReference("123"); 

Which of the two strings will be garbage collected first, when the JVM needs memory?


Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net

No comments: