如何在 Java 中打印 ByteBuffer 的地址?

How to print the address of a ByteBuffer in Java?

我想查看 Java 中 ByteBuffer 的地址。我用谷歌搜索发现 here 提到了 ByteBuffer 的地址如下:

// print address of ByteBuffer being queued
int bbAddress = System.identityHashCode(byteBuffer);

我不确定,所以我需要有人可以帮助我验证。

谢谢。

您似乎想在日志文件中包含不同对象的唯一标识符。那么

System.identityHashCode(object);

是必经之路。在数组的情况下,

也是如此
System.identityHashCode(array) == array.hashCode()

请注意,这是一个唯一标识符,而不是像 C 中的指针地址那样的内存地址。

'As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.',这也适用于 System.identityHashcode()