Jack said:
Oh, I see now. Thanks. Casting never actually changes any object, it
only can change the type of the *reference* to that object. So, if we
speak of casting objects, that is essentially incorrect.
Yes, precisely!
What does the reference to an object look like anyway? How is it laid
out in memory?
This is technically unspecified. In practice, it's pretty much always
just a memory address; 32 or 64 bits, depending on the JVM platform.
There must be a field for name, for type, and a pointer
to the actual object in memory. What else?
Nope. The name is either:
1. Only significant at compile time, if it's a local variable, OR
2. If it's a field, associated with the containing class, OR
3. Non-existent anywhere, if it's an intermediate result.
So there isn't a name as part of the reference itself. In other words,
if I write:
class Test
{
private void a(Test p1)
{
}
public static void main(String[] args)
{
Test t = new Test();
a(t);
}
}
There is NOTHING that I could do to the parameter p1, at runtime, that
would tell me that the reference was originally called t. In fact, if
you compile without debug information, then neither "p1" nor "t" will
appear in the class file.
Types are similar. They are used by the compiler to check type safety.
By the time the code is written out to the class file, there is no sign
of the type you declared for that reference.
So, only the memory address is actually stored.
Is there any way, using any
tool, that I can see this myself?
Not easily. You could cause a core dump in native code for a Java
process in UNIX, but you've have a heck of a time tracing everything
through JIT'ed machine code to figure out what's happening.
You could agree to the SCSL and download source code for the JDK, as
well. That might be easier than the first option, but it's definitely
non-trivial to find the relevant code.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation