Java Interfaces and Memory Allocation

Consider:

public SomeClass implements SomeInterface{...} SomeClass obj = new SomeClass(); SomeInterface x = obj; 

I am trying to associate line 3 with my simplest understanding of memory management. I know that the memory location represented by "obj" just contains a pointer to the SomeClass memory location. Assuming I'm using a 64-bit JVM, then up to 64 bits are allocated for the obj pointer. What is created in memory when the JRE implements x? Is this just a 64-bit pointer to SomeClass?

+4
source share
3 answers

Each reference to an object occupies the same amount of memory, regardless of how you declare it.

So x and obj are two links for links that just point to the same thing.

+3
source

In simple sentences, sentences refer to the same memory in Java as declared.

Stack and heap for memory allocation , this will help you figure out how to do this

enter image description here work.

+1
source

There is no actual memory overhead; SomeInterface at this point is simply building the language by typing x for later verification by the compiler.

0
source

All Articles