C
chucky
When testing two objects for equality (with == operator), the
references are compared. But it is not allowed to compare the objects
(references) with <, <=, >, >= operators.
Is it possible somehow to get the address of an object and then
compare it?
What I want to do is to have each instance of my class unique, so that
two objects are equal only if they are the same object. This is easy,
I can make equals method to compare references. But I would like to
use these objects in TreeSet/TreeMap and therefore implement the
compareTo method in a way that would be consistent with respect to
equals.
I can think of one workaround: the class having an additional integer
field id and creating the instances with a synchronized factory
method. But just want to know if it is possible without that extra
field.
Thanks for replies!
Tomas
references are compared. But it is not allowed to compare the objects
(references) with <, <=, >, >= operators.
Is it possible somehow to get the address of an object and then
compare it?
What I want to do is to have each instance of my class unique, so that
two objects are equal only if they are the same object. This is easy,
I can make equals method to compare references. But I would like to
use these objects in TreeSet/TreeMap and therefore implement the
compareTo method in a way that would be consistent with respect to
equals.
I can think of one workaround: the class having an additional integer
field id and creating the instances with a synchronized factory
method. But just want to know if it is possible without that extra
field.
Thanks for replies!
Tomas