GHads mind

developers thoughts & finds

Create unique identifiers in Java – UUID

leave a comment »

Sometimes you have the need for unique identifiers. Luckily, Java provides an easy way for creating globaly unique strings:


import java.util.UUID;

public class GenerateUUID {

  public static final void main(String... aArgs){
    //generate random UUIDs
    UUID idOne = UUID.randomUUID();
    UUID idTwo = UUID.randomUUID();
    log("UUID One: " + idOne);
    log("UUID Two: " + idTwo);
  }

  private static void log(Object aObject){
    System.out.println( String.valueOf(aObject) );
  }
}

outputs:

UUID One: 067e6162-3b6f-4ae2-a171-2470b63dff00
UUID Two: 54947df8-0e9e-4471-a2f9-9af509fb5889

Thanks to Java Practices for providing this usefull piece of information!
Advertisements

Written by ghads

September 19, 2008 at 9:52 am

Posted in Java development

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: