Java UUIDWhat is UUID?UUID stands for Universally Unique Identifier. UUID are standardized by the Open Software Foundation (OSF). It is a part of Distributive Computing Environment (DCE). A UUID is 36 characters long unique number. It is also known as a Globally Unique Identifier (GUID). Java UUID Class
Java UUID Representation The representation of the UUID uses hex digits. Java UUID is made up of hex digit along with four hyphens (-). It is 36 characters long unique number, including four hyphens. A UUID may be nil, in which all bits are set to zero.
Java UUID UsageIt is used to create the following:
Leach-Salz variant: The variant field contain a value that identifies the layout of the UUID. The representation of a variant 2 (Leach-Salz) UUID is as follows:
The bit layout defined above is valid only for a UUID with a variant value of 2 that indicates the Leach-Salz variant. Types of UUID There are four types of UUIDs:
Version 3 and 5The UUID version 3 and 5 generate the UUID using the hash of namespace and name. The namespace identifiers are UUIDs like Domain Name System (DNS), URLs, and OIDs (Object Identifiers), etc. The difference between UUID version 3 and UUID version 5 is of the hashing algorithm. Version 3 uses MD5 (128-bits) algorithm, while version 5 uses SHA-1 (160-bits) algorithm. We trim the resulting hash to 128-bits and then replace 4 bit for the version and 2 bit for the variant. Java does not provide the implementation of version 5. Version 4The UUID version 4 implementation uses random numbers as the source. It uses an unpredictable value as the seed to generate a random number to reduce the chance of collisions. UUID VariantsThe most used variant is 2 (Leach-Salz). There are the following types of variants:
In the above figure "V" indicates the UUID version and the "R" (1-3) indicate the UUID variant. Java UUID ConstructorWe can generate different types of UUID by using constructor. The syntax of the constructor is: The argument mostSigBits is used for the MSB (64-bit) of the UUID and leastSigBits becomes LSB (64-bit) of the UUID. Methods of UUID classUUID ramdomUUID() methodThe randomUUID() method randomly generate the UUID. Whenever we run the program, it generates a new UUID. The signature of the method is: The method returns the randomly generated UUID. Example The following example generates a random UUID. Output c6a8669e-ee95-4c42-9ef6-4a9b61380164 UUID version() method The version associated with this UUID. The version number describe how this UUID was generated. The method returns the version number of the UUID. The signature of the method is: Example Output UUID version is: 1 UUID version is: 3 UUID variant() method The variant number associated with this UUID. It describes the layout of the UUID. The method returns the variant number of the UUID. The signature of the method is: Example Output UUID variant is: 2 UUID variant is: 7 UUID node() method The node() method returns the node value associated with the UUID. It is a 48-bit node value. It is constructed from the field of this UUID. It holds the IEEE 802 address (MAC Address) of the machine. It throws the UnsupportedOperationException if the UUID is not a version 1 UUID. The signature of the method is: Example Output Node value: 55075465998336 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India