Java UUID toString() 方法



描述

Java UUID toString() 方法用於返回表示此 UUID 的 String 物件。

宣告

以下是 java.util.UUID.toString() 方法的宣告。

public String toString()

引數

返回值

方法呼叫返回此 UUID 的字串表示形式。

異常

使用標準格式化字串示例獲取 UUID 的字串表示形式

以下示例演示瞭如何使用 Java UUID toString() 方法獲取此 UUID 的字串表示形式。我們使用給定的字串建立了一個 UUID 物件。然後,我們使用 toString() 方法列印了此 UUID 物件的字串表示形式。

package com.tutorialspoint;

import java.util.UUID;

public class UUIDDemo {
   public static void main(String[] args) {

      // creating UUID      
      UUID x = UUID.fromString("38400000-8cf0-11bd-b23e-10b96e4ef00d");

      // getting string representation
      System.out.println("string representation: "+x.toString());    
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

string representation: 38400000-8cf0-11bd-b23e-10b96e4ef00d

獲取使用隨機生成的 UUID 的字串表示形式示例

以下示例演示瞭如何使用 Java UUID toString() 方法獲取此 UUID 的字串表示形式。我們使用 randomUUID() 方法建立了一個 UUID 物件。然後,我們使用 toString() 方法列印了此 UUID 物件的字串表示形式。

package com.tutorialspoint;

import java.util.UUID;

public class UUIDDemo {
   public static void main(String[] args) {

      // creating UUID      
      UUID x = UUID.randomUUID();

      // getting string representation
      System.out.println("string representation: "+x.toString());
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

string representation: 8f5f2803-2c9b-4c6c-90d4-6d53828d245b

獲取使用位元組生成的 UUID 的字串表示形式示例

以下示例演示瞭如何使用 Java UUID toString() 方法獲取此 UUID 的字串表示形式。我們使用 nameUUIDFromBytes() 方法建立了一個 UUID 物件。然後,我們使用 toString() 方法列印了此 UUID 物件的字串表示形式。

package com.tutorialspoint;

import java.util.UUID;

public class UUIDDemo {
   public static void main(String[] args) {

      // creating byte array 
      byte[] nbyte = {10,20,30};

      // creating UUID from byte     
      UUID uid = UUID.nameUUIDFromBytes(nbyte);

      // getting string representation
      System.out.println("string representation: "+uid.toString());
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

string representation: 7f49b84d-0bbc-38e9-a493-718013baace6
java_util_uuid.htm
廣告

© . All rights reserved.