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.
source share