|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "How To Create UUID in Java?" |
| 4 | +author: gaurav |
| 5 | +image: assets/images/2020-12-23/create-uuid-in-java.png |
| 6 | +categories: [ Java, Core Java, String] |
| 7 | +description: In this article you will see, how to create UUID in Java. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## Introduction |
| 12 | + |
| 13 | +UUID, a universally unique identifier is a 128-bit number used to identify information in computer systems. |
| 14 | + |
| 15 | +UUID is made of hex digits along with 4 hyphen ("-") symbols. The length of a UUID is 36 characters. |
| 16 | + |
| 17 | +There are 5 types of UUID but mostly version 4 i.e. Randomly Generated UUID is used. |
| 18 | + |
| 19 | +We are going to create the version 4 UUID here. |
| 20 | + |
| 21 | +### Example UUID |
| 22 | + |
| 23 | +df6fdea1-10c3-474c-ae62-e63def80de0b |
| 24 | + |
| 25 | +## How to create UUID in Java? |
| 26 | + |
| 27 | +Creating a Randomly Generated UUID (version 4) is really easy in Java. |
| 28 | + |
| 29 | +UUID Class is present in `java.util` package. And it has the static method `randomUUID()` which returns the randomly generated UUID. |
| 30 | + |
| 31 | +The example is given below. |
| 32 | + |
| 33 | +```java |
| 34 | +import java.util.UUID; |
| 35 | + |
| 36 | +/** |
| 37 | + * A Java program to create a Randomly Generated UUID i.e. Version 4 UUID |
| 38 | + * @author Gaurav Kukade at coderolls.com |
| 39 | + * |
| 40 | + */ |
| 41 | +public class CreateUUID { |
| 42 | + |
| 43 | + public static void main(String[] args) { |
| 44 | + |
| 45 | + // creating random uuid i.e. version 4 UUID |
| 46 | + UUID uuid = UUID.randomUUID(); |
| 47 | + |
| 48 | + System.out.println("Printing the randomly generated UUID value......\n"); |
| 49 | + |
| 50 | + System.out.println("uuid: "+uuid); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +``` |
| 57 | +Output |
| 58 | +```java |
| 59 | +Printing the randomly generated UUID value...... |
| 60 | + |
| 61 | +uuid: e3aed661-ccc2-42fd-ad69-734fee350cd2 |
| 62 | + |
| 63 | +``` |
| 64 | + |
| 65 | +Get the [above code as GitHub Gist](https://gist.github.com/gauravkukade/395f314c549969bd300d72c7e032dbcb). |
| 66 | + |
| 67 | +-------- |
| 68 | + |
| 69 | +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. |
| 70 | + |
| 71 | +If you found this article worth, support me by [giving a cup of Coffee ☕](https://www.paypal.me/GauravKukade) |
| 72 | + |
| 73 | +#### Related Articles |
| 74 | + |
| 75 | +- [Learn About Java String Pool And intern() Method](https://coderolls.com/java-string-pool-and-intern-method/) |
| 76 | +- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/) |
| 77 | +- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) |
| 78 | +- [Compare Two Strings Lexicographically In Java](https://coderolls.com/compare-two-strings-lexicographically-in-java/) |
| 79 | +- [Difference Between StringBuffer and StringBuilder class](https://coderolls.com/difference-between-stringbuffer-and-stringbuilder/) |
| 80 | + |
| 81 | +- [8 Basic GIT Commands Every Newbie Developer Must Know](https://coderolls.com/basic-git-commands/) |
0 commit comments