java.util.UUID.randomUUID().toString() length

前端 未结 3 1900
刺人心
刺人心 2020-12-14 13:59

Does java.util.UUID.randomUUID().toString() length always equal to 36?

I was not able to find info on that. Here it is said only the following:

3条回答
  •  天涯浪人
    2020-12-14 14:48

    Does java.util.UUID.randomUUID().toString() length always equal to 36?

    Yes!! it is.

    A UUID actually a 128 bit value (2 long). To represent 128 bit into hex string there will be 128/4=32 char (each char is 4bit long). In string format it also contains 4 (-) that's why the length is 36.

    example: 54947df8-0e9e-4471-a2f9-9af509fb5889

    32 hex char + 4 hyphen char = 36 char. So the length will be always same.


    Update:

    I do not know what type 4 means in the case.?

    FYI: There are several ways of generating UUID. Here type 4 means this uuid is generated using a random or pseudo-random number. From wiki - Universally_unique_identifier#Versions:

    Versions

    For both variants 1 and 2, five "versions" are defined in the standards, and each version may be more appropriate than the others in specific use cases. Version is indicated by the M in the string representation.

    Version 1 UUIDs are generated from a time and a node id (usually the MAC address);

    version 2 UUIDs are generated from an identifier (usually a group or user id), time, and a node id;

    versions 3 and 5 produce deterministic UUIDs generated by hashing a namespace identifier and name;

    and version 4 UUIDs are generated using a random or pseudo-random number.

提交回复
热议问题