Java generation UUID Universal unique identification code

Source: Internet
Author: User
Tags session id

I. Overview of the UUID

The UUID meaning is a universal unique identifier (universally unique Identifier), which is a software construction standard and is also organized by the Open Software Foundation, OSF, in a distributed computing environment ( Distributed Computing Environment, DCE) part of the field.

The purpose of the UUID is to allow all elements in a distributed system to have unique identification information without the need to specify the information through the central control terminal. In this way, everyone can create a UUID that does not clash with others. In such a case, there is no need to consider the name duplication problem when the database is established.

Currently the most widely used UUID is Microsoft's globally Unique Identifiers (GUIDs), while other important applications are the Linux ext2/ext3 file system, LUKS encryption partition, GNOME, KDE, Mac OS X and more.

The only drawback to the UUID is that the resulting string will be longer .

Second, the UUID composition

The UUID guarantees that all machines in the same time and space are unique. Typically, the platform provides the generated APIs. Based on the standard calculations developed by the Open Software Foundation (OSF), Ethernet card addresses, nanosecond time, chip ID codes, and many possible numbers are used.
UUID components:
(1) The current date and time, the first part of the UUID is related to the time, if you generate a UUID after a few seconds to generate a UUID, then the first part is different, the rest is the same.
(2) clock sequence.
(3) Globally unique IEEE machine identification number, if there is a network card, from the network card MAC address obtained, no network card is obtained in other ways.


In ColdFusion, the UUID can be easily generated with the Createuuid () function,
The format is: xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx (8-4-4-16), where each x is a hexadecimal number in the range of 0-9 or a-f.

The standard UUID format is: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12);

Third, Java uses UUID

The Java.util.UUID class represents a constant, universally unique identifier (UUID). Here are the key points about the UUID:

    • A UUID represents a 128-bit value.

    • It is used to create random file names in the Web application's session ID, transaction ID, and so on.

    • There are four different basic types of UUID: Time-based, DCE security, name-based, and randomly generated UUID.

The following is a declaration of the Java.util.UUID class:

 Public Final class UUID    extends Object       Implements Serializable, comparable<uuid>

Use of UUID:

// The generated ID 752D231C-E5A3-871C-65D4-142DFA562CB4 contains a horizontal bar, which is useful for generating the database's primary key ID. uuid uuid  =  uuid.randomuuid (); String id=uuid.tostring (); ID=id.replace ("-", ""); // Replace the middle bar  .

Method Summary
 int clockSequence()
The value of the clock sequence associated with this UUID.
 int compareTo(UUID val)
Compares this UUID to a specified UUID.
 boolean equals(Object obj)
Compares this object to a specified object.
static UUID fromString(String name)
toString()creates a UUIDbased on the string standard representation described in the method.
 long getLeastSignificantBits()
Returns the lowest valid 64 bits in the 128-bit value of this UUID.
 long getMostSignificantBits()
Returns the most significant 64 bits in the 128-bit value of this UUID.
 int hashCode()
Returns the UUID hash code for this.
static UUID nameUUIDFromBytes(byte[] name)
Gets a static factory of type 3 (name-based)UUID , based on the specified byte array.
 long node()
The node value associated with this UUID.
static UUID randomUUID()
Gets the static factory of type 4 (pseudo-randomly generated) UUID.
 long timestamp()
The timestamp value associated with this UUID.
 String toString()
Returns the object that represents this UUID String .
 int variant()
The variant number associated with this UUID .
 int version()
The version number associated with this UUID .

Java generation UUID Universal unique identification code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.