java 如何在Java中打印变量的地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33315184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:30:24  来源:igfitidea点击:

How to print address of a variable in Java

javacollectionslinked-list

提问by h8pathak

As an address of a variable (i.e. int a) in C can be obtained by &a.

作为 C 中变量(即 int a)的地址可以通过&a.

How can this be done in Java? For Eg. The elements of a linked list are non-contiguous. How can we print the address of the elements of a linked list created using the Collections Framework?

这如何在 Java 中完成?例如。链表的元素是不连续的。我们如何打印使用集合框架创建的链表元素的地址?

If the above thing is not possible, how can we show that the elements of an array are contiguous whereas that of a linked list are not?

如果上面的事情是不可能的,我们如何证明数组的元素是连续的,而链表的元素不是?

采纳答案by Durandal

There is no element in the java language that allows you the get the address of anything and more importantlythere is no language element ever requiring an address for anything. Thats why there is no address-of operator in java. The whole language is designed to work without.

java 语言中没有任何元素可以让您获得任何东西的地址,更重要的是,没有任何语言元素需要任何东西的地址。这就是java中没有地址运算符的原因。整个语言被设计为无需工作。

The whole concept of memory address is abstracted in java; the closestyou can get is a reference, but that is limited to actual objects. While the reference's value is actually a memory address (or at least something easily convertible into a memory address, see compressed OOPS), there is no wayof converting that value to anything else. And again there is no needto ever do this, except maybe for satisfying your curiosity.

内存地址的整个概念是在java中抽象出来的;您可以获得的最接近的引用,但这仅限于实际对象。虽然引用的值实际上是一个内存地址(或者至少可以轻松转换为内存地址,请参阅压缩的 OOPS),但无法将该值转换为其他任何值。再一次没有必要这样做,除非可能是为了满足你的好奇心。

It is however possible, by using the (attention: not portable, not meant for actual public use!) java.sun.misc.Unsafe, this class allows converting the value of a reference to a long (representing a memory address). Example for using it can be found here: https://dzone.com/articles/understanding-sunmiscunsafe.

然而,通过使用(注意:不可移植,不适合实际公共使用!)java.sun.misc.Unsafe,这个类允许将引用的值转换为 long(表示内存地址)。可以在此处找到使用它的示例:https: //dzone.com/articles/understanding-sunmiscunsafe

回答by Vladimir Petrakovich

It is impossible to get an address of any primitive variable or object in Java. All that you can do is get default object hash code (which usually related to its address, but it is not guaranteed) with System.identityHashCode(a).

在 Java 中不可能获得任何原始变量或对象的地址。您所能做的就是使用System.identityHashCode(a).

回答by jwenting

There is no way to get the memory address of anything in Java. All that is hidden by the JVM, you never have to worry about it.
Even if you could get it, you couldn't do anything with it...

Instead, you identify objects by other means, typically by implementing their equals() and hashCode() methods.

在 Java 中没有办法获得任何东西的内存地址。所有这些都被 JVM 隐藏了,您永远不必担心。
即使你能得到它,你也不能用它做任何事情……

相反,你通过其他方式识别对象,通常是通过实现它们的 equals() 和 hashCode() 方法。

回答by Rohit

THIS DATA IS TAKEN FROM JAVA DOC>

此数据取自 JAVA DOC>

THIS add() FUNCTIONALITY WHEN USED WITH ARRAYLIST public boolean add(E e) Appends the specified element to the end of this list. Specified by: add in interface Collection Specified by: add in interface List Overrides: add in class AbstractList

此 add() 与 ARRAYLIST 一起使用时的功能 public boolean add(E e) 将指定的元素附加到此列表的末尾。指定者:在接口集合中添加指定者:在接口列表中添加覆盖:在类 AbstractList 中添加

THIS add() FUNCTIONALITY WHEN USED WITH LINKEDLIST

与链接列表一起使用时,此 add() 功能

public boolean add(E e) Appends the specified element to the end of this list. This method is equivalent to addLast(E). Specified by: add in interface Collection Specified by: add in interface Deque Specified by: add in interface List Specified by: add in interface Queue

public boolean add(E e) 将指定的元素附加到此列表的末尾。此方法等效于 addLast(E)。指定者:在接口Collection中添加 指定者:在接口Deque中添加 指定者:在接口List中添加 指定者:在接口Queue中添加

IN BOTH CASES YOU CAN SEE add() WORKING DIFFERENTLY. IN ARRAYLIST IT USES ONLY LIST AND COLLECTION INTERFACE.

在这两种情况下,您都可以看到 add() 的工作方式不同。在数组列表中,它仅使用列表和收集界面。

BUT IN LINKEDLIST CASE IT USING DEQUE AND QUEUE INTERFACE means internally java is actually creating a node and the inserting it non-contiguously .

但在链接列表的情况下,它使用双端队列和队列接口意味着在内部 java 实际上正在创建一个节点并非连续地插入它。

Hope you get your answer.

希望你能得到答案。