uuid java 长度,java.util.UUID.randomUUID()。toString()长度

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:

public static UUID randomUUID()

Static factory to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.

Returns:

A randomly generated UUID

And that type 4 tells me nothing. I do not know what type 4 means in the case.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值