Global unique identifiers in Java, the easy way

Originally published November 07, 2011.
Information provided in this post may be out of date.

A recent attempt at creating a unique identifier at work was shot down in flames by Findbugs. My naïve attempt wasn’t very thoughtful but seemed to work.


Findbugs indicated there was a possibility that I might end up with a negative value even though I “cleverly” used Math.abs().

RV: Bad attempt to compute absolute value of signed random integer (RV_ABSOLUTE_VALUE_OF_RANDOM_INT) If the number returned by the random number generator is Integer.MIN_VALUE, then the result will be negative as well

Even though I didn’t need this random id to be perfect – the unintended side-effect wasn’t a technical problem since I used it as a String – I still wanted to fix it. Stackoverflow to the rescue.

A question-and-answer from Stackoverflow pointed the way to the solution: java.util.UUID. Somehow, I missed that Sun had added Java’s own universally unique identifier (UUID) generator in Java 1.5.

In the end, I happily replaced my homemade id generator with Java’s.


The output of which is something like 1c312843-8903-411f-88b2-ff1b92ca80ba.

Cross-posted at