سؤال

Consider:

public SomeClass implements SomeInterface{...}

SomeClass obj = new SomeClass();

SomeInterface x = obj;

I am trying to relate line 3 to my very basic understanding of memory management. I know the memory location represented by "obj" just contains a pointer to the memory location of SomeClass. Assuming I am using a 64bit JVM, then up to 64 bits are allocated for the "obj" pointer. What is created in memory when the JRE implements x? Is it just a 64bit pointer to SomeClass?

هل كانت مفيدة؟

المحلول

Every object reference takes up the same amount of memory, no matter how you declare it.

So x and obj are two distict references, which just happen to point at the same thing.

نصائح أخرى

In simple sentence references take same memory in Java however declared.

Stack and Heap for Memory Allocation this will help you understand in detail how it

enter image description here works.

There's no actual memory overhead, the SomeInterface declaration is at this point simply a language construct, typing x for later checking by the compiler.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top