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?
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) .
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.
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.
You can't print just the name of a variable.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With