问题描述:

In C I might say

printf("%p",0x11194);

*((int *) 0x11194) = 30240;

(In this example I know that there is an integer at that memory location)

Please tell me, how would I do the same in Java? Btw I may have completely misunderstood how memory works

网友答案:

You can do this in Java using sun.misc.Unsafe and ByteBuffer.

However for normal Java usage you don't do this at all. Java will give you an object reference, which is handle to an object. It doesn't point to one place in memory (the JVM can move objects in memory without you knowing/caring) and you can't perform arithmetic on them (unlike pointers)

网友答案:

You have not misunderstood how memory works; you have merely assumed that Java will let you touch it. It won't. The memory and pointers are still there, but must be accessed through proper Object creation and dereferentiation, as opposed to direct pointer memory access.

On the bright side, you don't really have to worry about freeing memory that you no longer need though.

相关阅读:
Top