What are the chances of getting exactly the same link to an object twice

I sometimes assume that if oldObject != newObject , then the object has changed - which seems like a fair assumption in most cases, but is this really a bad assumption?

In short, in what situation can the following code print "Same!"

 static WeakReference<Object> oldO = null; ... Object o = new Object(); oldO = new WeakReference(o); // Do some stuff with o - could take hours or even days to complete. ... // Discard o (or let it go out of scope). o = null; // More stuff - could be hours or days later. ... o = new Object(); // Later still. if ( o == oldO.get() ) { System.out.println("Same!"); } 

I understand that this is really possible, because a reference to an object is, in fact, the memory address of the object (or maybe in some JVM). But how is this possible? Are we talking about a decade of experience before this actually happens?

Added

My apologies - suppose oldO is some form of weak link that doesn't stop it from oldO up. Perhaps this is weak , as the code suggests (now), or the link is stored in a database or file somewhere.

+6
source share
8 answers

(I reply that I think that you really wanted to find out, not the specific fragment that you have)

It depends on the implementation. The contract for linking to objects is that while the object is still alive, no other object will compare with it. This means that after the object collects garbage, the virtual machine can reuse the same object reference.

A Java implementation can use integer increment to refer to an object, in which case you can only get the same object reference when the counter goes back to 0. Another implementation can use memory, which makes it more likely for the same The link to be reused. In any case, you must define your own object identifier, if that matters.

+4
source

will never be the same. oldO always refers to the original object, so it will never be discarded, and the new object will not be able to have the same address.

UPDATE: It seems that the response has been updated to indicate that oldO is a weak link. In this case, when the object leaves, the oldO reference will become null. This means that it will never match another object in the JVM.

+4
source

It is impossible for them to be equal. You still have a reference to the old object ( oldO ), so it will never be dropped.

+3
source

o == oldO means o - the same memory address as oldO . Thus, this cannot happen if at some point you are not doing either o = oldO or oldO = o . By transitivity, making foo = o; oldO = foo foo = o; oldO = foo or any equivalent, of course, will achieve the same result.

+1
source

Firstly, the memory address does not matter. Java is not C. Object identification is an implementation of the JVM β€” it may or may not rely on the memory address, but most likely this does not happen, since the JVM is free to move the object in memory, but must retain its identity.

But it doesn’t matter. because you are holding a link to the original object, the second cannot be the "same" object.

+1
source

It will never happen.

Your first object ( oldO ) is stored in a specific memory location.

Your second object will be systematically referenced elsewhere in memory if it refers to oldO.

So oldO == o will compare both memory addresses, which will always be different.

If you are looking for oldO, it will be garbage collection, and you can create a new object at the same address. But you cannot compare it to oldO because it has been dereferenced.

0
source

Having a link to your old object, you prevent garbage collection, so you do not allow a bit of memory to be available for the new object, so they could never be equal.

I wondered if you used SoftReference so that you can reference the old object, allowing it to collect garbage BUT:

1) I assume that after the old object is assembled, the SoftReference will be null and

2) it artificially tries to force the situation, so it actually proves nothing :-)

0
source

According to the documentation, weak links will be removed when the object is garbage collected. It does not indicate what β€œclean up” means, but it appears to be null. If it is actually set to null, null will never be declared == for any reference to an object, regardless of its location in memory.

0
source

All Articles