13

As an address of a variable (i.e. 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?

1
  • This wouldn't make sense in Java because the Garbage Collector can move objects around.
    – Jimmy T.
    Oct 24, 2015 at 11:18

4 Answers 4

14

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

13

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.

4

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.