Does anybody know to get the memory addresses of an array indexes? (like in c)
-
3You can't get memory addresses, what are you really trying to accomplish?Mark Elliot– Mark Elliot2011-01-13 02:31:45 +00:00Commented Jan 13, 2011 at 2:31
-
3If you absolutely need that information in Java, you probably chose the wrong platform for your work.Etienne de Martel– Etienne de Martel2011-01-13 02:33:18 +00:00Commented Jan 13, 2011 at 2:33
2 Answers
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.
Comments
You can't. Java doesn't have direct memory access.