File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Memory Address of an Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Memory Address of an Array" Watch "Memory Address of an Array" New topic
Author

Memory Address of an Array

Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24
Hi Experts,

Whats is the Java statement to know the memory addresses of the elements in an array?

Regards,
Faraz
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Faraz Alig wrote:Hi Experts,

Whats is the Java statement to know the memory addresses of the elements in an array?


Do you mean as is done in C and C++? AFAIK, this concept doesn't exist in Java.
Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24
Hi Pete,

Yes, somewhat similar to what we have in C.
In C, we use address-of" operator (unary &) to find the address.

Do we have a similar such things here as well in Java?

- Faraz
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Faraz Alig wrote:
Do we have a similar such things here as well in Java?


As already mentioned, pointer arithmetic doesn't exist in Java. However, an array dereference is incredibly fast, so there is nothing wrong with your standard index arithmetic for arrays.

Henry
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd ask why you *want* to know the memory address of an array. Are you trying to do something with JNI?
Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24
David Newton wrote:I'd ask why you *want* to know the memory address of an array. Are you trying to do something with JNI?


Hi David,

I am new to Java and just learning Arrays right now.


Suppose list1 and list2 are the reference to two different arrays
For the expression list2 = list1;

then both point to the same array.

Just to clarify this i want to know the memory address as this will be a solid evidence to prove this point.

Thanks,
Faraz
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Faraz Alig wrote:
Suppose list1 and list2 are the reference to two different arrays
For the expression list2 = list1;

then both point to the same array.


For any reference variable, setting ref1 = ref2 will mean that they refer to the same object. To see if this is true, simply check if ref1 == ref2.
Md Ibrahim
Greenhorn

Joined: Mar 24, 2010
Posts: 14
hi faraz,

I dont know if you are using any IDE like eclipse, If you do then its very easy to debug and find at runtime
the unique ID's created at runtime for variables and objects.

For eg:
Test aTest1 = new Test();
Test aTest2= new Test();

here aTest1 will have an ID say 34322 and aTest2 something different 34644

but if you execute below statement

aTest2 = aTest1;

after this line if you see both test1 and test2 will point to same reference id 34322

same applies for all other attributes in java.
By,

Ibrahim


Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24
Hi Ibrahim,

This point of yours is indeed useful for the verification of such things.

Thanks,
Faraz
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

In Java, you don't need to know the memory address of an object. Java does not have pointers like you have in C, and you never need to deal with things such as the addresses of objects in memory. So forget about things like memory addresses when you're programming in Java - it doesn't apply and will confuse you if you try to apply those ideas from C to Java.

As Pete says, you can use the == operator to check if two variables refer to the same object in memory.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Memory Address of an Array
 
Similar Threads
Convert Integer List to int array
Array doubt
Output of a simple code
Sorting and Collection Help
How much memory this array will utilize?