References in OOP
1. What is a reference? #
Reference, a concept of OOP language, is a variable contains a value of the address which refers (or points) to where in memory an object is stored.

To make it easier to understand, let me give an example: Your house is somewhere in the city, and a friend cannot know it until you give him a visit card which contains the house’s address. In this case, the card is a reference and the house is an actual space or value in memory.
However, be careful with the another concept, Pointer in C++ programming. Actually, reference and pointer seem to do some same or similar things. Especially, we can use them for accessing an object.

Pointer, on the other hand, is different from reference in some cases. If you are interested in C++ programming, please check more here
2. Where is a reference? #
References are stored in a Stack and the objects which they refer to are stored in a Heap.
For example:
Integer baz = 123;
String bar = "123";
3. What types of reference? #
There are 4 main types of references: Strong, Weak, Soft and Phantom references, in term of how strongly reachable the reference is.
Strong reference: just like we declare a variable as usual.
For example:
People david = new People();In this example, david is a strong reference, refers to the space for storing new People(). Therefore, the space of memory will never be cleaned or freed when there is still a strong reference referring to it.
As a result, Remember the rule: The allocated memory will be cleared away if no reference refers to it (which means it is not used anymore).
For instance:
People david = new People();
david = null;With this code, the allocated space for new People() will be recycled by OS because david currently refers to null object, doesn’t refer to new People() anymore. In other words, OS will understand that this space of memory is unused and then should be cleared to save memory.
Back to the rule “The allocated memory will be cleared away if no reference refers to it”. So the question here is if there is one reference referring to the allocated memory, will it be cleared?
The answer is Yes or No, depends on what kind of the reference we use. Likewise, if it is a strong reference, the answer must be No, the allocated space will be kept if there is a strong reference referring to it. In contrast, the Yes answer will happens sometimes as the next part below.
Soft reference:
The answer here is Yes or No when we use soft reference. Because a soft reference is strong enough to keep the allocated memory from recycling of OS, until OS’s memory is low. In short, softly referenced objects are cleared in response to memory demand.
An example for soft reference declaration:SoftReference softRef = new SoftReference(new Object()); // Creating a strong reference of the object Object getObject = softRef.get();As a result, we usually use a soft reference for caching and it’s a safe way to optimize memory. On the other hand, don’t use it for any static value that we need to get whenever we want.
Weak reference:
Again, the question: if there is one reference referring to the allocated memory, will it be cleared?
The answer here is Yes or No when we use weak reference. Because a weak reference is only strong enough to keep the allocated memory, until a memory management process appears (we call it Garbage Collector, GC, let me talk about it later or you can see here How Garbage Collection Really Works). In short, weakly referenced objects are cleared when GC process works.WeakReference weakRef = new WeakReference(new Object()); // Creating a strong reference of the object Object strongRef = weakRef.get();Since a weakly referenced object is discarded by GC process at any time, is a weak reference useful?
Of course, yes. Let me write another post about it later because it’s kind of long to explain here. Please check this article to see Plugging memory leaks with weak references
Phantom reference:
It is the weakest reference. An object is phantomly referenced after it has been finalized, but before its allocated memory has been reclaimed.
We rarely use phantom reference to implement a feature of our application, because it’s just for technical issue. So I don’t want to make you more confused with it, but if you are interested in, please check the article here.
In the next post, I would like to introduce some useful cases in which being aware of how to use reference can help us a lot to save memory and avoid the Out Of Memory error (OOM).
