I was thinking about mutable objects and how they're weird (but very cool).
Question: can a mutable object not equal itself?
Only caveat here is that obviously you must override equals
method, otherwise the default checks pointer equality which will (obviously) always be satisfied.
Edit to Question
Alright, I've thoroughly confused everyone, take a look at this program:
import java.util.Random;
public class EqualsTest {
private static final Random RANDOM = new Random(System.currentTimeMillis());
private int value = 0;
public static void main(String... args) {
System.out.println("Starting program...");
final EqualsTest test = new EqualsTest();
final Thread modify = new Thread(new Runnable() {
public void run() {
while (true)
test.value = RANDOM.nextInt(Integer.MAX_VALUE);
}
});
final Thread equals = new Thread(new Runnable() {
public void run() {
while (true)
if (!test.equals(test)) {
System.out.println("test didn't equal test");
}
}
});
modify.start();
equals.start();
}
@Override
public boolean equals(Object e) {
if (!(e instanceof EqualsTest))
return false;
final EqualsTest obj = (EqualsTest) e;
return this.value == obj.value;
}
}