Types of References in Java

Hello, learners Today I’m going to discuss the types of references present in Java.

Java has mainly four types of references.

1.Strong references

2.Weak references

3.Soft references

4.Phantom references

Types of References

Strong reference(Default type reference) –  We use strong references normally in Java .we can create an object and then assigns it to a reference.  if this happens the object has a strong reference, it can never be garbage collected.

Weak Reference – Weak reference doesn’t have any strong reference and despite having storage capacity in memory it can be terminated by the garbage collector.

Soft reference – If an object has a soft reference, then the garbage collector claims the memory of the object wherein the memory is not enough then the garbage collector needs to free some memory. To get the object from a soft reference, it can invoke the get() method. If the object is not garbage collector, it returns the object, otherwise, it returns null.

Phantom Reference – If an object does not match with any of the above-discussed references then it is absolutely phantom reference.  You cannot access phantom references directly and using get() method it always returns null.

Code

import java.lang.ref.*;

public class Reference {
  
       private String text ="Hi Codespeeders !";
       public String getText() {
              return text;
       }
       
       public void setText(String text) {
              this.text = text;
       }
       @Override
       public String toString() {
              return "Reference [text=" + text + "]";
       }
       
       public void strongReference()
       {
              Reference example = new Reference();
              System.out.println(example);
       }
       
       public void softReference()
       {
              SoftReference<Reference> example = new SoftReference<Reference>(getRefrence());
              System.out.println("Soft refrence :: " + example.get());
       }
       
       public void weakReference()
       {
              int counter=0;
              WeakReference<Reference> example = new WeakReference<Reference>(getRefrence());
              while(example.get()!=null)
              {
                     counter++;
                     System.gc();
                     System.out.println("Deleted Weak Reference : " + counter + example.get());
              }
       }
       
       public void phantomReference() throws InterruptedException
       {
              final ReferenceQueue queue = new ReferenceQueue();
              PhantomReference<Reference> example = new PhantomReference<Reference>(getRefrence(),queue);
              System.gc();
              queue.remove();
              System.out.println("Deleted Phantom Reference :  ");
       }
       
       private Reference getRefrence()
       {
              return new Reference();
       }
       
       
       public static void main(String[] args) {
    	  Reference example = new Reference();
              example.strongReference();
              example.softReference();
              example.weakReference();
              try {
                     example.phantomReference();
              } catch (InterruptedException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
       }
}

Output –

TypesOfReference [text=Hi Codespeeders !]
Soft refrence :: TypesOfReference [text=Hi Codespeeders !]
Deleted Weak Reference: 1null
Deleted Phantom Reference

Also read: MessageFormat equals() method in Java

Leave a Reply

Your email address will not be published. Required fields are marked *