An implementation of generating GUID based on Java

  • 2020-04-01 01:57:13
  • OfStack

GUID is a number 128 bits long, usually in hexadecimal notation. The core idea of the algorithm is to combine the network card of the machine, local time, and an random number to generate GUID. In theory, if a machine produced 10 million guids per second, it would be guaranteed (probabilistically) not to repeat itself for 3, 240 years.

package com.cn.str;
import java.util.UUID;

public class CreateGUID {

 public static final String GenerateGUID(){
  UUID uuid = UUID.randomUUID();
  return uuid.toString();  
 }
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println(GenerateGUID());
 }
}

UUID is a new class in 1.5, under java.util, which can be used to generate a so-called globally unique ID

Related articles: