|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Your Guide To UUID In Java" |
| 4 | +author: gaurav |
| 5 | +image: assets/images/2020-12-19/uuid.png |
| 6 | +categories: [ Java, Core Java, String] |
| 7 | +description: In this article you will learn what is UUID and about the Java UUID class. |
| 8 | +featured: false |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +In this article we will see, |
| 13 | +- What is UUID? |
| 14 | +- Java UUID class |
| 15 | + |
| 16 | +## What is UUID? |
| 17 | + |
| 18 | +UUID, a universally unique identifier is a 128-bit number used to identify information in computer systems. |
| 19 | + |
| 20 | +UUID is made of hex digits along with 4 hyphen ("-") symbols. The length of a UUID is 36 characters. |
| 21 | + |
| 22 | +There are 4 types of UUID |
| 23 | + |
| 24 | +1. Time-based UUID (Version 1) |
| 25 | +2. DCE Security UUID (Version 2) |
| 26 | +3. Name-based UUID (Version 3 and 5) |
| 27 | +4. Randomly Generated UUID (Version 4) |
| 28 | + |
| 29 | +Mostly Randomly Generated UUID i.e. UUID Version 4 is used. |
| 30 | + |
| 31 | +UUID Version 4 uses random numbers as a source. It uses an unpredictable value as the seed to generate random numbers to reduce the chance of collisions |
| 32 | + |
| 33 | +There are 4 types of UUID variant |
| 34 | + |
| 35 | +- **0:** It is reserved for NCS backward compatibility |
| 36 | +- **2:** Leach-Salz |
| 37 | +- **6:** It is reserved for Microsoft backward compatibility |
| 38 | +- **7:** It is reserved for future definition. |
| 39 | + |
| 40 | +Mostly the variant 2 is used. |
| 41 | + |
| 42 | +UUID can be used in the following cases |
| 43 | + |
| 44 | + - As a primary key of database table |
| 45 | +- To create session id for web application |
| 46 | +- To represent as transaction id |
| 47 | +- To create a random file name |
| 48 | + |
| 49 | +### Example UUID |
| 50 | + |
| 51 | +df6fdea1-10c3-474c-ae62-e63def80de0b |
| 52 | + |
| 53 | + |
| 54 | +## Java UUID class |
| 55 | + |
| 56 | +UUID Class belongs to the `java.util` package. It represents an immutable universally unique identifier (UUID). |
| 57 | + |
| 58 | +UUID Constructor |
| 59 | + |
| 60 | +```java |
| 61 | +`[UUID](https://docs.oracle.com/javase/8/docs/api/java/util/UUID.html#UUID-long-long-)(long mostSigBits, long leastSigBits)` |
| 62 | +``` |
| 63 | +Constructs a new `UUID` using the specified data. |
| 64 | + |
| 65 | +### Static methods |
| 66 | +There are three static methods of a UUID class, |
| 67 | + |
| 68 | +```java |
| 69 | +UUID.fromString(String name) |
| 70 | +``` |
| 71 | +The above method creates a UUID from the string standard representation as described in the [toString()](https://docs.oracle.com/javase/8/docs/api/java/util/UUID.html#toString--) method. |
| 72 | + |
| 73 | +```java |
| 74 | +UUID.nameUUIDFromBytes(byte[] name) |
| 75 | +``` |
| 76 | +This method is used to create a version 3 (name based) UUID based on the specified byte array. |
| 77 | + |
| 78 | +```java |
| 79 | +UUID.randomUUID() |
| 80 | +``` |
| 81 | +This method is used to create a version 4 (pseudo randomly generated) UUID. |
| 82 | + |
| 83 | +### Instance methods |
| 84 | + |
| 85 | +Also there are a few instance methods of a UUID class. |
| 86 | + |
| 87 | +```java |
| 88 | +clockSequence() |
| 89 | +``` |
| 90 | +This method is used to get the the clock sequence value associated with this UUID. |
| 91 | + |
| 92 | +This method returns the clock sequence value as `int`. |
| 93 | + |
| 94 | +```java |
| 95 | +compareTo(UUID val) |
| 96 | +``` |
| 97 | +This method is used to compare this UUID with the specified UUID ( the one received as method param i.e. `val` |
| 98 | + |
| 99 | +This method returns -1, 0 or 1 as this `UUID` is less than, equal to, or greater than `val` |
| 100 | + |
| 101 | +```java |
| 102 | +equals(Object obj) |
| 103 | +``` |
| 104 | +This method simply compares this object to the specified object. It return the result in `boolean` |
| 105 | + |
| 106 | +```java |
| 107 | +node() |
| 108 | +``` |
| 109 | +This method returns the node value (`long`) associated with this UUID. |
| 110 | + |
| 111 | +```java |
| 112 | +timestamp() |
| 113 | +``` |
| 114 | +This method returns the timestamp value (`long`) associated with this UUID. |
| 115 | + |
| 116 | +```java |
| 117 | +toString() |
| 118 | +``` |
| 119 | +This method returns a `String` object representing this UUID. |
| 120 | + |
| 121 | +```java |
| 122 | +variant() |
| 123 | +``` |
| 124 | +This method returns the variant number (`int`) associated with this UUID. |
| 125 | + |
| 126 | +```java |
| 127 | +version() |
| 128 | +``` |
| 129 | +This method returns the version number (`int`) associated with this UUID. |
| 130 | + |
| 131 | +-------- |
| 132 | +You can visit my [YouTube channel 'coderolls'](https://www.youtube.com/channel/UCl31HHUdQbSHOQfc9L-wo3w?view_as=subscriber?sub_confirmation=1) to find more video tutorials. |
| 133 | + |
| 134 | +If you found this article worth, support me by [giving a cup of Coffee ☕](https://www.paypal.me/GauravKukade) |
| 135 | + |
| 136 | +#### Related Articles |
| 137 | + |
| 138 | +- [Learn About Java String Pool And intern() Method](https://coderolls.com/java-string-pool-and-intern-method/) |
| 139 | +- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/) |
| 140 | +- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) |
| 141 | +- [Compare Two Strings Lexicographically In Java](https://coderolls.com/compare-two-strings-lexicographically-in-java/) |
| 142 | +- [Introduction to Java Technology (Language and Platform)](https://coderolls.com/java-introduction/) |
| 143 | + |
0 commit comments