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

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

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

Java中UUID类toString()方法一般用来获取这个UUID的字符串表示。

句法:

public String toString()

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

返回值:此方法返回一个字符串值,它是此 UUID 的字符串表示形式。

下面的程序说明了 toString() 方法的工作:

方案一:

// Java code to illustrate toString() 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 string representation
        System.out.println("The string representation is: "
                           + UUID_1.toString());
    }
}
输出:
UUID: 58e0a7d7-eebc-11d8-9669-0800200c9a66
The string representation is: 58e0a7d7-eebc-11d8-9669-0800200c9a66

方案二:

// Java code to illustrate toString() 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 toString Value
        System.out.println("The string representation is: "
                           + UUID_1.toString());
    }
}
输出:
UUID: 5fc03087-d265-11e7-b8c6-83e29cd24f4c
The string representation is: 5fc03087-d265-11e7-b8c6-83e29cd24f4c