2

Does anybody know to get the memory addresses of an array indexes? (like in c)

2
  • 3
    You can't get memory addresses, what are you really trying to accomplish? Commented Jan 13, 2011 at 2:31
  • 3
    If you absolutely need that information in Java, you probably chose the wrong platform for your work. Commented Jan 13, 2011 at 2:33

2 Answers 2

5

There is no programmer-realizable notion of an "address" in Java. In a language like C or C++, objects' identities are equated with their address - two objects are the same object if they live in the same memory location. In Java, this notion of identity is decoupled from the object's address. This allows some optimizations that are not possible in C++. For example, the garbage collector could, in theory, move objects around in memory to avoid fragmentation, so long as it modifies references so they point to the right location. Because memory addresses can't be accessed directly by the programmer, this operation is permissible. In C++, it wouldn't work, because the compiler couldn't tell if a particular bit pattern in memory was some sort of encoded pointer.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't. Java doesn't have direct memory access.

2 Comments

As an Alternative, can i do it using an array objects?
Depends on what you are trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.