I am having a little confusion about the PhantomReference in java. When I looked in online articles most of them mentioned that the PhantomReference object will return null after creation even if we invoke get() on the reference and also it mentioned the objects are phantomly reachable will be inserted into the ReferenceQueue when the PhantomReference is removed from the memory.
I have tried the following code example but I have a confusion about the result.
Equipment equipment = new Equipment();
ReferenceQueue queue = new ReferenceQueue();
PhantomReference pr = new PhantomReference(equipment, queue);
System.out.println(pr.get());
System.out.println(queue.poll());
Above two statements prints null. If the pr.get() returns null does it mean the object referred by pr is garbage collected? If it is so, why the object is still haven't added to the priority queue?
Can anybody help please me to clarify this. I apologies from you if my understanding is wrong regarding the PhantomReference and the ReferenceQueue.
Please describe these two terms simply as well
thanks a lot