Java 5 UUID

It is very common to use unique identifiers in web application for identifying unique users or some other usage, till now I was always
writing an program using some random generator code for them. It is always hard to do that, as those unique identifiers should not
repeat them self in future, else it will lead in data integrity issue later on.

Recently i got to knw about UUID class out of box from Java UUID
UUID static class provide 3 static method to genretae UUID’s. UUID generate are from UUID are actually universally unique identifiers.

1) UUID.randomUUID()

randomUUID() will result some thing like following,

Result : 8284e82d-a2ff-4120-a684-6d7166991b05

2) UUID.fromString(“18-23-58-96-96”)

This method requires an input containing long number separated by ‘-‘. It mean first secton would most significant bit and section 4 would be least signt bits that would be used
for creating UUID.

Result : 00000018-0023-0058-0096-000000000096

import java.util.UUID;

public class UUIDExample {

  public static final void main(String... aArgs){
    //generate example random UUIDs
    System.out.println("Example UUID One: " + UUID.randomUUID());
    System.out.println("Example UUID Two: " + UUID.fromString("18-23-58-96-96"));

Example run :
>java -cp . UUIDExample
Example UUID One: 067e6162-3b6f-4ae2-a171-2470b63dff00
Example UUID Two: 54947df8-0e9e-4471-a2f9-9af509fb5889

Java bug

I am Java professional, working on Enterprise Java from last 8 years.

More Posts - Website

Follow Me: