Just start using java.util.UUID
. My question is if I have two UUID variables, say u1 and u2, and I would like to check if they are equal, can I safely use expression u1 == u2
or have to write u1.equals(u2)
? assuming both are not null.
BTW, I am using its randomUUID
method to create new UUID values, but I think this should not be matter.
I wonder as UUID is unique, each value could be a singleton, then it is safe to use u1 == u2
.
void method1(UUID u1, UUID u2) {
// I know it is always safe to use equal method
if (u1.equals(u2)){
// do something
}
// is it safe to use ==
if (u1 == u2) {
// do something
}
}
==
is enough if you are really talking about variables. What is the real scenario?==
onObject
s is tantamount to a bug in Java. Occasions for its use are rare.==
for UUIDs, but only if you had some very strong guarantees about how they're used (never created via deserialization; never copied; never provided by any outside code). It's not worth it. Use.equals
.