• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Memory Address of an Array

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ibrahim,

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

Thanks,
Faraz
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic