Skip to content

Add new blogpost #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -17,4 +17,5 @@ group :jekyll_plugins do
gem 'jekyll-archives'
gem 'kramdown'
gem 'rouge'
gem 'kramdown-parser-gfm'
end
7 changes: 6 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -44,6 +44,8 @@ GEM
listen (~> 3.0)
kramdown (2.3.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
liquid (4.0.3)
listen (3.2.1)
rb-fsevent (~> 0.10, >= 0.10.3)
@@ -63,6 +65,7 @@ GEM
sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
wdm (0.1.1)

PLATFORMS
ruby
@@ -75,7 +78,9 @@ DEPENDENCIES
jekyll-seo-tag
jekyll-sitemap
kramdown
kramdown-parser-gfm
rouge
wdm (>= 0.1.0)

BUNDLED WITH
2.0.2
2.1.4
143 changes: 143 additions & 0 deletions _posts/2020-12-19-your-guide-to-uuid-in -java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
---
layout: post
title: "Your Guide To UUID In Java"
author: gaurav
image: assets/images/2020-12-19/uuid.png
categories: [ Java, Core Java, String]
description: In this article you will learn what is UUID and about the Java UUID class.
featured: false

---

In this article we will see,
- What is UUID?
- Java UUID class

## What is UUID?

UUID, a universally unique identifier is a 128-bit number used to identify information in computer systems.

UUID is made of hex digits along with 4 hyphen ("-") symbols. The length of a UUID is 36 characters.

There are 4 types of UUID

1. Time-based UUID (Version 1)
2. DCE Security UUID (Version 2)
3. Name-based UUID (Version 3 and 5)
4. Randomly Generated UUID (Version 4)

Mostly Randomly Generated UUID i.e. UUID Version 4 is used.

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

There are 4 types of UUID variant

- **0:** It is reserved for NCS backward compatibility
- **2:** Leach-Salz
- **6:** It is reserved for Microsoft backward compatibility
- **7:** It is reserved for future definition.

Mostly the variant 2 is used.

UUID can be used in the following cases

- As a primary key of database table
- To create session id for web application
- To represent as transaction id
- To create a random file name

### Example UUID

df6fdea1-10c3-474c-ae62-e63def80de0b

![Structure of UUID](/assets/images/2020-12-19/uuid.png)
## Java UUID class

UUID Class belongs to the `java.util` package. It represents an immutable universally unique identifier (UUID).

UUID Constructor

```java
`[UUID](https://docs.oracle.com/javase/8/docs/api/java/util/UUID.html#UUID-long-long-)(long mostSigBits, long leastSigBits)`
```
Constructs a new `UUID` using the specified data.

### Static methods
There are three static methods of a UUID class,

```java
UUID.fromString(String name)
```
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.

```java
UUID.nameUUIDFromBytes(byte[] name)
```
This method is used to create a version 3 (name based) UUID based on the specified byte array.

```java
UUID.randomUUID()
```
This method is used to create a version 4 (pseudo randomly generated) UUID.

### Instance methods

Also there are a few instance methods of a UUID class.

```java
clockSequence()
```
This method is used to get the the clock sequence value associated with this UUID.

This method returns the clock sequence value as `int`.

```java
compareTo(UUID val)
```
This method is used to compare this UUID with the specified UUID ( the one received as method param i.e. `val`

This method returns -1, 0 or 1 as this `UUID` is less than, equal to, or greater than `val`

```java
equals(Object obj)
```
This method simply compares this object to the specified object. It return the result in `boolean`

```java
node()
```
This method returns the node value (`long`) associated with this UUID.

```java
timestamp()
```
This method returns the timestamp value (`long`) associated with this UUID.

```java
toString()
```
This method returns a `String` object representing this UUID.

```java
variant()
```
This method returns the variant number (`int`) associated with this UUID.

```java
version()
```
This method returns the version number (`int`) associated with this UUID.

--------
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.

If you found this article worth, support me by [giving a cup of Coffee ☕](https://www.paypal.me/GauravKukade)

#### Related Articles

- [Learn About Java String Pool And intern() Method](https://coderolls.com/java-string-pool-and-intern-method/)
- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/)
- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/)
- [Compare Two Strings Lexicographically In Java](https://coderolls.com/compare-two-strings-lexicographically-in-java/)
- [Introduction to Java Technology (Language and Platform)](https://coderolls.com/java-introduction/)

Binary file added assets/images/2020-12-19/uuid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.