Re: what is the 'this' keyword



K Gaur <gaurkuber@xxxxxxxxx> wrote:
[The literal, but unhelpful, answer to how "this" is actually
implemented in Java is that I would expect the compiler to have some
sort of hash table indexed by the keywords.]

well if it is that way that did help

I think you may have misinterpreted what was intended to be a humorous
remark. What Patricia means is that when a compiler is reading the
source code, it probably uses a hash table of some kind to recognize
that the four characters 't', 'h', 'i', and 's' form the keyword "this".
This is unlikely to have answered your real question, which is
presumably why Patricia said it is an unhelpful answer.

Perhaps if I give you one possible answer, that will help. You should
realize that I'm not giving you "the" answer, because implementation
techniques can vary between compilers and Java virtual machines. That
said, one way that the compiler can give you a "this" pointer from
inside an instance method is that when it calls the method, it passes
along a pointer to the object as a sort of hidden parameter.
Specifically, a likely way to implement instance method calls in Java
would look something like this:

1. Follow the object pointer, and look at a predetermined offset to
find a pointer to a class descriptor.
2. Look in some predetermined offset in the class descriptor
to find a pointer to the code for the desired method.
3. Push the object pointer to the stack.
4. Push the parameters to the stack.
5. Call the method's code.

So the object pointer is on the stack from step 3, at a known offset
from the stack pointer (because the method implementation knows how
large its arguments are). When you write "this", the (probably JIT)
compiler generates code to look at that location and get the object
pointer there.

There are lot of different ways this could be written, but that's a
reasonable one. (Also, there are other less common uses for the this
pointer; if you're asking about uses of this to chain constructors, or
something else, then let us know.)

--
Chris Smith
.



Relevant Pages

  • Re: Java outperforms C++?
    ... > absolutely no relevance to which language is faster. ... > it invalidate their reasons for why Java turns out to be faster. ... On Intel they found that the Java performance was very reasonable compared to C, and that Java was faster than at least one C compiler. ... Pointer aliasing is a problem in C, where int* and char* might be common. ...
    (microsoft.public.vc.language)
  • Re: Mex Overflow Error Using Free Borland Compiler
    ... >>>would clean up a lot of code by eliminating the pointer dereferences. ... >>>guaranteed by the standard and should not be relied on. ... >>>You can try the GNU C compiler available on mingw or cygwin. ... >>>the Constraints are then both operands shall have arithmetic type. ...
    (comp.soft-sys.matlab)
  • Re: Anybody here endure C/Cpp? (.h to .inc conversion)
    ... Pascal or Stdcall convention... ... "PFNGLPOINTPARAMETERFEXTPROC" to be a type that's a pointer to a function ... DWORD, using a 32-bit compiler, with an address in it...adding the ... this is working on the premise that OpenGL does it like most others ...
    (alt.lang.asm)
  • Re: Why the compiler applies sign extension to unsigned data?
    ... I want the value of p to be made unsigned *before* the cast. ... want the cast to generate a sign extension. ... ULONG_PTR is ULONG and casting the pointer to ULONG_PTR first is the ... on the compiler and on the knowledge of the size of the ptr variable, ...
    (microsoft.public.development.device.drivers)
  • Re: OO programming - illumination? - whoopsie
    ... > represented by a host of loosely related arrays. ... > compiler and the memory allocators. ... A pointer is usually a word. ... organs can be further modeled to include cells and so on and so forth. ...
    (comp.lang.java.programmer)