55

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
   }
}
4
  • Your post is confusing. Of course, == is enough if you are really talking about variables. What is the real scenario? May 11, 2014 at 5:01
  • the java doc does not answer this question. May 11, 2014 at 5:01
  • 1
    The presence of == on Objects is tantamount to a bug in Java. Occasions for its use are rare. May 11, 2014 at 5:08
  • 1
    You could use == 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. May 11, 2014 at 5:12

4 Answers 4

101

It depends: which type of equality do you want?

UUID a = new UUID(12345678, 87654321);
UUID b = new UUID(12345678, 87654321);
UUID c = new UUID(11111111, 22222222);

System.out.println(a == a); // returns true
System.out.println(a.equals(a)); // returns true

System.out.println(a == b); // returns false
System.out.println(a.equals(b)); // returns true

System.out.println(a == c); // returns false
System.out.println(a.equals(c)); // returns false

a == b is true only if a and b are the same object. If they are two identical objects, it will still be false.

a.equals(b) is true if a and b are the same UUID value - if their two parts are the same.

It's a rhetorical question, by the way. Almost always you want .equals. There isn't much use for == with UUIDs.

4
  • 1
    Ah, you are correct. I should have thought about writing such a piece of code myself to verify. Thanks. May 11, 2014 at 7:19
  • At beginning, I thought as each UUID value is unique, why not implement each value as a singleton. May 11, 2014 at 7:22
  • @NewyachtZhang first, new always creates a new object, so you'd need to use something else like (hypothetically) UUID.get. Also, presumably these "singletons" are created on demand (otherwise there would be 340,282,366,920,938,463,463,374,607,431,768,211,456 objects) so they're not actually singletons. Also, it would be a waste of time to track all the UUID objects in existence and search through them every time you use a UUID. Also, the only benefit is to let you use ==. Is it worth it? (If it was, they'd also do it with String)
    – user253751
    May 11, 2014 at 7:33
  • It's a rhetorical question by the way. Obviously you want .equals in this case.
    – user253751
    Dec 31, 2020 at 20:18
5

Well...no.

== against an object checks for reference equality. That is, it checks to see if these two objects are literally the same spot in memory.

.equals() will check for actual object equivalence. And, the Javadoc for UUID goes into great detail to explain when two UUID instances are equivalent.

0

Uncommonly they are no flyweight objects because of bad ood (and wont be for compatibility reasons)! So, its a mind-trap.

Only in flyweight objects who provide a static-factory-method to create instances only you can be quite sure take usage of == instead of .equals(. Unfortunately because you have a public constructor its no flyweight-pattern.

This means you indeed have to use .equals(.

0

The equals() method of UUID class in Java is used to check for the equality of one UUID with another. The method returns true if both the UUIDs are exactly identical bit by bit.

Syntax:

public boolean equals(Object uuidObj)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.