Java UUID getMostSignificantBits() 方法



描述

Java UUID getMostSignificantBits() 方法用於返回此 UUID 的 128 位值的最高有效 64 位。

宣告

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

public long getMostSignificantBits()

引數

返回值

方法呼叫返回此 UUID 的 128 位值的最高有效 64 位。

異常

使用標準格式字串示例生成 UUID 的最高有效 64 位

以下示例演示了 Java UUID getMostSignificantBits() 方法的使用,用於獲取此 UUID 的 128 位值的最高有效 64 位。我們使用給定的字串建立了一個 UUID 物件。然後,我們使用 getMostSignificantBits() 方法列印與 UUID 物件關聯的最高有效 64 位。

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 most significant 64 bits
      System.out.println("most significant 64 bits: "+x.getMostSignificantBits());    
   }    
}

輸出

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

most significant 64 bits: 4053239666997989821

使用隨機生成的 UUID 示例獲取最高有效 64 位

以下示例演示了 Java UUID getMostSignificantBits() 方法的使用,用於獲取此 UUID 的 128 位值的最高有效 64 位。我們使用 randomUUID() 方法建立了一個 UUID 物件。然後,我們使用 getMostSignificantBits() 方法列印與 UUID 物件關聯的最高有效 64 位。

package com.tutorialspoint;

import java.util.UUID;

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

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

      // getting most significant 64 bits
      System.out.println("most significant 64 bits: "+x.getMostSignificantBits());
   }    
}

輸出

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

most significant 64 bits: 9185635904161073452

使用位元組示例生成 UUID 的最高有效 64 位

以下示例演示了 Java UUID getMostSignificantBits() 方法的使用,用於獲取此 UUID 的 128 位值的最高有效 64 位。我們使用 nameUUIDFromBytes() 方法建立了一個 UUID 物件。然後,我們使用 getMostSignificantBits() 方法列印與 UUID 物件關聯的最高有效 64 位。

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 most significant 64 bits
      System.out.println("most significant 64 bits: "+uid.getMostSignificantBits());
   }    
}

輸出

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

most significant 64 bits: 9172064757165603049
java_util_uuid.htm
廣告