i’m writing some stuff in java and i ran into some problems lately. cut short, i need to compare an object i created to another instance of this very class i instantiazed before with different data.
looks like this:
a newA = null;
a oldA = null;
while(someBreakCondition) {
newA = new a();
newA.x = getSomeValue();
// now i want to compare the two objects
if(oldA != null) {
if(newA.time != oldA.time) {
// ...
}
}
// now my newA gets to be my oldA, since in the next iteration, a new newA is created
oldA = newA;
}
with a class a:
class a {
public long time;
public int x;
public a() {
time = System.currentTimeMillis;
}
}
the problem is, that i end up finding out that the values from newA are always equal to those from oldA. so i guess sth went wrong with passing the references of the objects in the last line of the loop…
i thought java always passes references of objects unless an explicit copy() is called?
if this does matter: this code is running on android – don’t know if the dalvik vm messes aroung with this…
I’m thinking you probably do have two different objects, but they both have the same value for
time. Current millis doesn’t quite have the precision to distinguish between two objects constructed in rapid succession, unless if your inner loop is long running. Even a Mhz processor will have iterations measured in microseconds, not milliseconds.