201 Java UUID Class

Introduction

The Java UUID class represents an immutable universally unique identifier (UUID). Following are the important points about UUID −

  • A UUID represents a 128-bit value.

  • It is used for for creating random file names, session id in web application, transaction id etc.

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

Class declaration

Following is the declaration for java.util.UUID class −

public final class UUID extends Object implements Serializable, Comparable<UUID>


Class constructors

Sr.No.Constructor & Description
1

UUID(long mostSigBits, long leastSigBits)

This constructor constructs a new UUID using the specified data.

Class methods

Sr.No.Method & Description
1int clockSequence()

This method shows the clock sequence value associated with this UUID.

2int compareTo(UUID val)

This method compares this UUID with the specified UUID.

3boolean equals(Object obj)

This method compares this object to the specified object.

4static UUID fromString(String name)

This method creates a UUID from the string standard representation.

5long getLeastSignificantBits()

This method returns the least significant 64 bits of this UUID's 128 bit value.

6long getMostSignificantBits()

This method returns the most significant 64 bits of this UUID's 128 bit value.

7int hashCode()

This method returns a hash code for this UUID.

8static UUID nameUUIDFromBytes(byte[] name)

Static factory to retrieve a type 3(name based) UUID based on the specified byte array.

9long node()

This method returns the node value associated with this UUID.

10static UUID randomUUID()

Static factory to retrieve a type 4(pseudo randomly generated) UUID.

11long timestamp()

This method returns the timestamp value associated with this UUID.

12String toString()

This method returns the String object representing this UUID.

13int variant()

This method returns the variant number associated with this UUID.

14int version()

This method removes the version number associated with this UUID.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object

Getting a UUID object from a Standard Formatted String Example

The following example shows usage of Java UUID fromString() method to get a UUID object from standard formatted string. We've created a UUID object using fromString() method and uid is printed.

package com.codeheap; import java.util.UUID; public class UUIDDemo { public static void main(String[] args) { // creating UUID UUID uid = UUID.fromString("38400000-8cf0-11bd-b23e-10b96e4ef00d"); // checking UUID value System.out.println("UUID value is: "+uid); } }

Output

Let us compile and run the above program, this will produce the following result.

UUID value is: 38400000-8cf0-11bd-b23e-10b96e4ef00d

Comments

Popular posts from this blog

79 HTML - Tables