• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Reference variable type question

 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranchers,

Consider a sample class called Employee and following declaration of a Employee variable,

Here's my understanding about this statement,

There are say 2000 houses(or memory blocks) within the runtime memory which can house 2000 instances of "Object"s. One such house, say No.42 contains the newly created Employee object, which can be housed because it is a subinstance of Object.
And what "emp" contains is this value 42, which on further re-direction fetches the actually object.

What kind of variable is this reference "emp"? Is there a special type say MemType, which is defined to be always a number which can range from 1 - 2000(just saying, in this case).

If there is indeed something like a Memtype, is there any inheritance/association between a super MemType and a sub Employee type.

Thanks in advance,
Praveen.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar M K wrote:Dear Ranchers,

Consider a sample class called Employee and following declaration of a Employee variable,

Here's my understanding about this statement,

There are say 2000 houses(or memory blocks) within the runtime memory which can house 2000 instances of "Object"s. One such house, say No.42 contains the newly created Employee object, which can be housed because it is a subinstance of Object.
And what "emp" contains is this value 42, which on further re-direction fetches the actually object.



What emp contains is not defined by the spec. It is implementation specific. In an abstract sense, it is the "address of the object", but that doesn't mean it's literally the memory address where the first member variable is. In one version, then value was a pointer to a pair of pointers, where one of that pair pointed to the class definition and the other to the object's data. In a later version, I think it was a pointer to a structure that contained a pointer to the class definition, followed by the object's member data. Note also that there's almost certain a least one more layer of indirection in there, so that objects can be moved around by the JVM and pages shuffled around by the OS without application pointers having to change.

What kind of variable is this reference "emp"?Is there a special type say MemType, which is defined to be always a number which can range from 1 - 2000(just saying, in this case).



It's entirely up to the JVM implementation.

If there is indeed something like a Memtype, is there any inheritance/association between a super MemType and a sub Employee type.



No. The type as far as the Java language is concerned is simply "reference". If there is a MemType, it exists only in the language and context in which the JVM is written. Whatever type the implementation is, that is not visible to our Java code.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes sense..i have no clue why i did not think of jvm specification while asking the question..thanks!
 
Marshal
Posts: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As JV says, it is implementation specific. You can find the virtual machine specification here, but it probably says nothing about the actual memory location of objects.
Remember the actual location will change as garbage collection occurs and the heap is reorganised.
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic