1

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

sun
  • 13
  • 3

2 Answers2

2

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)

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

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.

zebediah49
  • 7,467
  • 1
  • 33
  • 50