简体   繁体   中英

How to print address of a variable in Java

As an address of a variable (ie int a) in C can be obtained by &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?

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?

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) .

There is no element in the java language that allows you the get the address of anything and more importantly there 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.

The whole concept of memory address is abstracted in java; the closest you 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 way of converting that value to anything else. And again there is no need to ever do this, except maybe for satisfying your curiosity.

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 .

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.

THIS DATA IS TAKEN FROM 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

THIS add() FUNCTIONALITY WHEN USED WITH LINKEDLIST

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

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

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

Hope you get your answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM