Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print address of a variable in Java

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?

like image 728
h8pathak Avatar asked Oct 24 '15 06:10

h8pathak


People also ask

Can we print address of variable in Java?

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

How do I print a variable address?

To print the address of a variable, we use "%p" specifier in C programming language. There are two ways to get the address of the variable: By using "address of" (&) operator. By using pointer variable.

Can I get address of an object in Java?

The hashCode() method is native because in Java it is impossible to find the address of an object, so it uses native languages like C/C++ to find the address of the object. Use of hashCode() method: It returns a hash value that is used to search objects in a collection.

Can you print a variable name in Java?

You can't print just the name of a variable.


1 Answers

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

like image 95
Vladimir Petrakovich Avatar answered Oct 16 '22 04:10

Vladimir Petrakovich