Wednesday, July 3, 2024
HomeLanguagesJavaUUID variant() Method in Java with Examples

UUID variant() Method in Java with Examples

Every UUID has a variant number describing the generation of this UUID. The variant() method of UUID class in Java is generally used to get variant number associated with this UUID.

Syntax:

public int variant()

Parameters: This method does not take any parameter.

Return Value: This method returns a int value which is the variant number associated with this UUID.

Below programs illustrate the working of variant() method:

Program 1:




// Java code to illustrate variant() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Creating two UUIDs
        UUID UUID_1
            = UUID
                  .fromString(
                      "58e0a7d7-eebc-11d8-9669-0800200c9a66");
  
        // Displaying the UUID
        System.out.println("UUID: "
                           + UUID_1);
  
        // Displaying the variant number
        System.out.println("The variant number is: "
                           + UUID_1.variant());
    }
}


Output:

UUID: 58e0a7d7-eebc-11d8-9669-0800200c9a66
The variant number is: 2

Program 2:




// Java code to illustrate variant() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Creating two UUIDs
        UUID UUID_1
            = UUID
                  .fromString(
                      "5fc03087-d265-11e7-b8c6-83e29cd24f4c");
  
        // Displaying the UUID
        System.out.println("UUID: "
                           + UUID_1);
  
        // Displaying the variant Value
        System.out.println("The variant number is: "
                           + UUID_1.variant());
    }
}


Output:

UUID: 5fc03087-d265-11e7-b8c6-83e29cd24f4c
The variant number is: 2

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments