Core Java

Java Reference Types

In this article, we talk about the Java Reference Types. First of all, we interpret what is a reference and why we use it. Furthermore we analyze the reference types. Last but not least, we explain the difference between Pass by value and pass by reference.

1. Introduction

A reference is a value that is used to refer to another value. To do that you need an address that you can point to another address. In C++ this is called a pointer and it points to a memory address but in Java, the reference is more abstract. Reference types include: Interfaces, classes, arrays, annotations, and enums. The reference has four types: Strong, weak, soft, and phantom.

2. Technologies used

The example code in this article was built and run using:

  • Java 1.8.231(1.8.x will do fine)
  • Eclipse IDE for Enterprise Java Developers- Photon

3. Java Reference Types

3.1. Strong Type

The strong type is the most common type that we used in Java. We can say that we use a strong type when we point directly a variable to an object. So the developer can interact with the object through this variable. For example:

public class StrongR {
	public static class Example {
		 public void example(){
			 System.out.println("Hello world");
		 }
		}

public static void main(String args[]) {
    Example strongEx= new Example();
	strongEx=null;
}
}

There isn’t something to print.

When the object “strongEx” will be null then the object will be available for the garbage collector.

Java Reference - Strong reference
Strong reference

3.2. Weak Type

In different with the other types that they preserved in memory if an object has only weak reference attached to it when the garbage collector is running the weak type will reclaim the object even if the virtual machine has no more space. For example:

import java.lang.ref.WeakReference;

public class WeakR {
	public static class Example {
		 public void example(){
			 System.out.println("Hello world");
		 }
		}
	public static void main(String args[]) {
		
		Example weakEx= new Example();
		weakEx.example();
		WeakReference<Example> wr = new WeakReference<Example>(weakEx);
		weakEx=null;
		weakEx=wr.get();
	}
}

The output is:

Hello world 

As we can see with “get()” we can retrieve the object because of the weak reference.

Weak reference

3.3. Soft Type

This type is used as a last chance for an application to stay alive. The Soft type has the ability to change the default behavior of the garbage collector and allows it to maintain the objects without strong references until JVM uses all the memory. For example:

import java.lang.ref.SoftReference;


public class SoftR {
	public static class Example {
		 public void example(){
			 System.out.println("Hello world");
		 }
		}
	public static void main(String args[]) {
		Example SoftEx=new Example();
		SoftEx.example();
		SoftReference<Example> sr = new  SoftReference<Example>(SoftEx);
		SoftEx=null;
		SoftEx=sr.get();
	}
}
 

The output is:

Hello world 

As we can see with “get()” we can retrieve the object because of the soft reference.

Soft reference

3.4. Phantom Type

This type is used for two reasons. First, to determine the time that an object was removed from the memory and the second is to avoid the use of ‘finalize’ method that helps garbage collector to release memory. For example:

 import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

public class PhantomR {
	public static class Example {
		 public void example(){
			 System.out.println("Hello world");
		 }
		}
	public static void main(String args[]) {
		Example PhanEx=new Example();
		PhanEx.example();
		ReferenceQueue r = new ReferenceQueue(); 
		PhantomReference pr = new  PhantomReference(PhanEx, r);
		PhanEx=null;
		PhanEx=pr.get();
		
		PhanEx.example();
	}
}

The output is:

Hello world
Exception in thread "main" java.lang.NullPointerException
	at PhantomR.main(PhantomR.java:19)

In this example we can see that ReferenceQueue is used to keep the removing object before removing from memory. When we remove the object every print that has to do with this gives us runtime error.

4. Pass by value versus Pass by reference

When we want to pass by value basically it means that when we call a method the parameter values are copied to another variable and after that the copied object(variables) is passed.

When we want to pass by reference it means that when we call a method, the parameter is a reference to another variable and this parameter is passed.

5. Download the Complete Source Code

Here is the code from the examples we used.

Download
You can download the full source code of this example here: Java Reference Types

Ioannis Makrygiannakis

John is an undergraduate student at the Department of Informatics of Athens University of Economics and Business. He is specialized in Databases, Knowledge Management, Information Systems and Information Security. In his free time he loves learning new things with regard to programming and network security.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button