Sobes.tech
Senior

How much does a pointer weigh? What is it?

sobes.tech AI

Answer from AI

A pointer is a variable that stores an address in memory, pointing to the location of another object or value. In Java, there are no explicit pointers, as the language manages object references automatically.

In the context of Java, we can talk about object references, which are essentially pointers, but without pointer arithmetic and direct memory access.

The size of a pointer (reference) depends on the JVM architecture and platform:

  • On 32-bit JVMs, a reference usually takes 4 bytes.
  • On 64-bit JVMs, it is 8 bytes, but pointer compression (Compressed OOPs) can be used, making the reference size 4 bytes to save memory.

Thus, the exact size of a reference in Java depends on the JVM and configuration, but it is usually 4 or 8 bytes.