Does using the 'this' keyword affect Java performance?

混江龙づ霸主 提交于 2019-12-01 04:16:20

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.

No it does not.

The code with or without this keyword after compilation is exactly the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!