Reference Strengths
This lesson explains the different reference types in Java.
We'll cover the following...
What are the different reference types in Java?
Note that in the above code if instead of creating a String object using new, we initialized the str variable using a string literal like str = "Educative.io"; then the program behavior would be different. The String literal isn't allocated on the heap rather it lives in a special area called the String pool. The String pool consists of string literals that can be reused and aren't removed from the pool even when there may be no reference to them. Therefore, if you run the same program initializing the str variable with a string literal, the print message would be different.
What are soft references?
Now we'll nullify the variable i = null and run the GC and this time the variable should be enqueued in the reference queue we passed in.