tags:

views:

50

answers:

2

Internally speaking, when you cast an object to a strongly typed object like:

User u = (User)o;

Where is the type of the object stored? is it stored in another location or within the memory block where the actual object is stored?

(I don't know much about this topic, so I could very well be asking a question that doesn't make complete sense, but I hope you get the gist of it)

+3  A: 

The header of each object contains a reference to the type of the most derived implementation class. Much like a "vtbl" in C++. Typically this will be a "C" structure rather than a Java object. I believe Azul's (64-bit) version of Hotspot use a 32-bit compact pointer to shorten the header size.

Tom Hawtin - tackline
A: 

Without overwhelming you with detail, the internal representation of a Java class has something called a constant pool that stores descriptors for classes and methods. These are symbolic links at first that are resolved on demand (ie at first access).

From the JVM spec, describing linking:

Linking is the process of taking a binary form of a class or interface type and combining it into the runtime state of the Java virtual machine, so that it can be executed. A class or interface type is always loaded before it is linked.

So, the actual type information for the referenced type is stored separately from the class in which you reference it.

If you're interested, here is a link to what the Class info looks like: http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#1221

danben