Overhead of 'this' keyword

247 views Asked by At

This seems like it could be a common question but I searched SO and Google and couldn't find quite what I'm looking for:

What is the overhead of calls to the this keyword in Java? I know in C++ there is some minimal overhead due to dereferencing the current object pointer. Does Java incur the same kind of overhead? Is it less optimal to make multiple calls to this. It's mostly a question of readability vs. optimization.

4

There are 4 answers

0
Michael Berry On BEST ANSWER

None whatsoever. They both produce exactly the same bytecode. For example, this:

package test;

public class T {

    int a=0;

    public T() {
        System.out.println(a); //this line
    }

    public static void main(String[] args) {
        new T();
    }

}

...produces:

public class test.T {
  int a;

  public test.T();
    Code:
       0: aload_0       
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0       
       5: iconst_0      
       6: putfield      #2                  // Field a:I
       9: getstatic     #3                  // Field     java/lang/System.out:Ljava/io/PrintStream;
      12: aload_0       
      13: getfield      #2                  // Field a:I
      16: invokevirtual #4                  // Method java/io/PrintStream.println:(I)V
      19: return        

  public static void main(java.lang.String[]);
    Code:
       0: new           #5                  // class test/T
       3: dup           
       4: invokespecial #6                  // Method "<init>":()V
       7: pop           
       8: return        
}

...regardless of whether the line marked this line uses a or this.a. (Try it if you like - compile the above code both ways and compare both class files with javap -c.) Considering they produce exactly the same bytecode, there's no way there can be differences in performance.

0
Martijn Courteaux On

There is no difference. If you call in class A from method a() to method b(), these are exactly the same:

public void a()
{
    this.b();
    b();
}

The overhead you are talking about is when it dereferences the pointer to the object to read or write variables. Imagine you want to write to class A the classmember a, you would do:

A x = new A();
x.a = 3;

Here, you encounter the overhead: when writing to x.a, it has to find out where that exactly is. To find that out, it has to make the sum of x and the offset of the variable a inside that class. So, it is probably making a sum like: x + 0x10. Now, it can write to the address that corresponds with the sum. That is the overhead you were talking about.

0
Matt On

If you mean something like using

private String myString;
public void doStringStuff() {
    this.myString xxxxx;
}

instead of

private String myString;
public void doStringStuff() {
    myString xxxxx;
}

then the bytecode will be the same, but it will compile a tiny tiny fraction faster, as the compiler does not need to check through local variables looking for a match.

0
Maroun On

Probably the same bytecode will be generated if you use this where you don't really have to.

But sometimes you must use this keyword (For example when you have name conflicts):

private int a;
public MyClass(int a) {
    this.a = a; //this is a must
}

For clarity, I think it's a matter of taste. Some programmers prefer to see it, some don't. Also note that this is a keyword, not an operator.

Tip: Don't waste time on performance issues for really small things..