Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using the 'this' keyword affect Java performance?

Does using the this keyword affect Java performance at all?

In this example:

class Prog {
  private int foo;

  Prog(int foo) {
    this.foo = foo;
  }
}

Is there performance overhead doing that over the following?:

class Prog {
  private int foo;

  Prog(int bar) {
    foo = bar;
  }
}

A couple of coworkers and I were discussing this earlier today and no one could come up with an answer the we all agreed on. Any definitive answer?

like image 217
Tux Avatar asked Feb 06 '14 22:02

Tux


People also ask

Should you always use this keyword Java?

It make no difference in the generated code. So use is up to company development guidelines or personal preference. It can help identify what is a member variable and what is not, it also help to use this-> as the ide or editor can do better code completion (depending on the age of the editor).

What happens if we don't use this keyword in Java?

Definition and Usage The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter). If you omit the keyword in the example above, the output would be "0" instead of "5".

Does final keyword increase performance?

The final keyword has a different purpose when applied to classes and methods. When we apply the final keyword to a class, then that class cannot be subclassed. When we apply it to a method, then that method cannot be overridden. There are no reported performance benefits of applying final to classes and methods.

Is it good to use final in Java?

Final should always be used for constants. It's even useful for short-lived variables (within a single method) when the rules for defining the variable are complicated.


1 Answers

No, not at all. It is just a different syntax for the same thing. It gets compiled into exactly the same piece of bytecode. So say it like a human: you are telling the compiler twice exactly the same thing what to do, in two different ways.


javap proves it. Here is with the this.:

{
  Prog(int);
    flags: 
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0       
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: aload_0       
         5: iload_1       
         6: putfield      #2                  // Field foo:I
         9: return        
      LineNumberTable:
        line 4: 0
        line 5: 4
        line 6: 9
}

And here is without this.:

{
  Prog2(int);
    flags: 
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0       
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: aload_0       
         5: iload_1       
         6: putfield      #2                  // Field foo:I
         9: return        
      LineNumberTable:
        line 4: 0
        line 5: 4
        line 6: 9
}

Only difference is the 2, but I had to choose a different name for the two test cases.

like image 185
Martijn Courteaux Avatar answered Oct 06 '22 00:10

Martijn Courteaux