📜  Java中的 UUID variant() 方法及示例

📅  最后修改于: 2022-05-13 01:54:34.729000             🧑  作者: Mango

Java中的 UUID variant() 方法及示例

每个 UUID 都有一个变体编号,用于描述此 UUID 的生成。 Java中UUID类variant()方法一般用于获取与该UUID关联的变体号。

句法:

public int variant()

参数:此方法不带任何参数。

返回值:此方法返回一个int 值,它是与此 UUID 关联的变体编号。

下面的程序说明了 variant() 方法的工作原理:

方案一:

// 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());
    }
}
输出:
UUID: 58e0a7d7-eebc-11d8-9669-0800200c9a66
The variant number is: 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());
    }
}
输出:
UUID: 5fc03087-d265-11e7-b8c6-83e29cd24f4c
The variant number is: 2