Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memory Address of an Array

 
Faraz Alig
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Experts,

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

Regards,
Faraz
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21507
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ibrahim,

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

Thanks,
Faraz
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic